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 [1] 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'

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()
>>> print 'HEY JUDE'.lower()
hey jude

If you want to get the length of the string, use len method.

>>> print len('hey jude')

To know more about the built-in methods in Python, you can refer to [2].

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:

>>> str(9)

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,

Curious Chick


[1] https://www.tutorialspoint.com/python/python_strings.htm

[2] https://docs.python.org/2/library/strings.html


Author: curiouschick

There are many things you may never know about me. But, two things you absolutely know when you visit my blog for the first time: I am a chick and really curious to know everything.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s