Last time we covered numeric data types, the ones holding numbers. Now it's time for the data type that holds characters, whether letters, words, or sentences. The data type we'll cover this time is String in Python 3.
Okee... let's just get going.
About the String Data Type in Python 3
A string in Python is a data type that holds one character or a run of them. A string is marked by single quotes ('...') or double quotes ("...") wrapped around that character or run of characters.
In Python the string data type is called by its short name, 'str'. That's the same way integer shortens to 'int'.
>>> a = 'ini adalah varibale string'
>>> b = "ini juga string"
>>> type(a)
<class 'str'>
>>> type(b)
<class 'str'>
Hmm... should I use single quotes or double quotes?
Well... there's actually no fixed rule for quotation marks. Some people even use both. Personally I prefer double quotes, because I'm used to them, hehe...
Okee.. moving on. Now try to guess the data type of variable k below.
>>> k = ""
Huh, just quotation marks?
Yes, that's exactly it. In programming this is usually called an empty string. Even though it's empty, the variable is still a valid variable.
>>> k = ""
>>> type(k)
<class 'str'>
See? So as long as there are quotation marks, the variable counts as a string even with no characters inside it.
What happens if you forget the quotation marks?
If you don't put quotation marks around string characters, Python reads those characters as a variable call. If the variable being called hasn't been declared, you get an error.
>>> a = ikan
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ikan' is not defined
You can see the error "name 'ikan' is not defined". It happens because Python reads 'ikan' in the value of variable a as a variable call.
String Data Type Operations in Python 3
Here are some operations on strings
Concatenation
Concatenation here means joining 2 or more strings into one.
>>> a = "hello" + " world"
>>> print(a)
hello world
>>> print("Inva" + "si" + "kode")
Invasikode
Character Indexing
Indexing means taking one character at a given position using a number. In Python, string indexes start at 0.
>>> nama = "Budi bin Adi"
>>> print(nama[0]) # accesses the 1st character
B
>>> print(nama[6]) # accesses the 7th character
i
>>> print(nama[10]) # accesses the 11th character
d
The square brackets after a variable name are called index access, here for a character inside a string. nama[0] accesses the first character of the nama variable, and so on.
Indexing is an operation that belongs to the list data type. A string can actually be classified as a list too, since it's built from several elements. That gets explained further on another occasion
Finding a String's Length
This one is a function that returns the length of a string.
>>> a = "Invasikode"
>>> len(a)
10
To find a string's length you only need the len() function. That function is very useful once you start dealing with loops.
Wrapping Up
That's it for this one, hope it helps
There's honestly still plenty to cover about strings, but we'll get to that on later occasions.
Next we'll cover logic data types in Python 3
See ya...