In the last tutorial, you knew how to do basic math with number and convert a number type to another one. In this tutorial, I will introduce a non-number type in Python called Strings.
Creating a string
There are two ways to create a string:
>>> 'Put the string here' 'Put the string here'
>>> "Put the string here" 'Put the string here'
It is simple, isn’t it? In Python, there is no distinction between single quoted and double quoted string. So, you can choose your favorite way to create it. For me, I prefer the first one.
In a Python program, a string is often used to display information or the value of a variable by using ‘print’ command. So, the string will has no sense if it stays alone. On the other hand, the string above is displayed on the screen every time you enter a string because you are using IDLE script. Nevertheless, string will not print itself when you run .py file unless you use ‘print’ command in your program.
Now, we will try using ‘print’ command to print a string on IDLE scripts. Do you still remember this command which was introduced in the first tutorial? It will be OK if you do not, because I will show it again.
>>> print 'I wanna be Spider-man' I wanna be Spider-man
String with special character
Now, try printing this string “Bobby’s cat was killed by “You-know-who””. It seems straightforward, right? However, if you try doing this
>>> print "Bobby's cat was killed by "You-know-who"" SyntaxError: invalid syntax
>>> print 'Bobby's cat was killed by "You-know-who"' SyntaxError: invalid syntax
you will get the error. It is caused by the Python recognize a string by looking at the characters between two single or double quotes. Thus, you may confuse the Python by using many quotes in a string.
So, to deal with this problem, you need to use a trick by adding character \ before the quote to make Python understand that you are using the special character in a string. The problem is solved now.
>>> print "Bobby's cat was killed by \"You-know-who\"" Bobby's cat was killed by "You-know-who"
>>> print 'Bobby\'s cat was killed by "You-know-who"' Bobby's cat was killed by "You-know-who"
Besides, there are many special characters which are used in string (refer to  for more information). For example, \t is used to make a horizontal tab while \v used to make a vertical tab.
>>> print 'Hello there' Hello there >>> print 'Hello \t there' Hello there
Operators with string
Different from number types, addition operator is used to concatenate two string.
>>> print 'Spider' + 'man' Spiderman
On the other hand, you can use *2 to double the string.
>>> print 'Dona '*2 Dona Dona
However, you cannot use *, -, or / with the aim of multiplying, subtracting, or dividing two strings.
Some useful built-in string methods
Sometimes, you can use these built-in methods below to manipulate your string easily.
>>> print 'hey jude'.capitalize() Hey jude
>>> print 'hey jude'.title() Hey Jude
>>> print 'hey jude'.upper() HEY JUDE
>>> print 'HEY JUDE'.lower() hey jude
If you want to get the length of the string, use len method.
>>> print len('hey jude') 8
To know more about the built-in methods in Python, you can refer to .
String with numbers
This is the final section and also the most important in this tutorial.
As I mentioned before, string is often used to display information or the value of a variable in a program. The problem is that, if you use this structure
>>> print 'The value of x is ' + 9 Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> print 'The value of x is ' + 9 TypeError: cannot concatenate 'str' and 'int' objects
you will get the error because you cannot add a string to a number. So, if you want to do this you will need to convert the number to the string by using:
And now, you get the result:
>>> print 'The value of x is ' + str(9) The value of x is 9
Moreover, to display the value of a variable, you can also use this structure:
>>> print 'The value of x is %d' %9 The value of x is 9
>>> print 'The value of x is ', 9 The value of x is 9
OK, with these simple knowledge, you can do somethings cool with string by yourself. In the next tutorial, I will introduce Python array type called list and tuple.
Hope you enjoy it,