Python has a lot of ways to represent strings, so it is useful to be familiar with them in case you ever find them in any python program.
Ordinary Strings
These are probably the most commonly used strings on python. You can enclose them using single or double quotes (‘ or “) and you can use a backslash to escape characters or to print special characters like a line break:
1
2
3
4
5
6
7
8
9
10
print "Hello\nWorld"
Hello
World
print 'Hello\nWorld'
Hello
World
print 'This is a backslash: \\'
This is a backslash: \
Long Strings
You can write long (multi-line) strings by encasing them on triple quotes (”’ or “””). This is very useful when you need to write very long statements because you don’t have to escape any quotes (‘ or “).
1
2
3
4
5
6
print '''She said: "Hello",
and I thought:
"This can't be real"'''
She said: "Hello",
and I thought:
"This can't be real"
You could do the same with double quotes:
1
2
3
4
5
6
7
print """She said: "Hello",
and I thought:
"This can't be real"
"""
She said: "Hello",
and I thought:
"This can't be real"
One thing to notice in my last example is that I entered a line break before closing the string. I did this because if I would have left them on the same line I would have had 4 consecutive quotes, and that would give an error. If you need your string to end with a quote and you don’t want the extra line break, you can escape the last quote that is part of the string:
1
2
3
4
5
6
print """She said: "Hello",
and I thought:
"This can't be real""""
She said: "Hello",
and I thought:
"This can't be real"
Raw Strings
Raw strings are useful when you don’t want to use backslashes to scape characters on your string. This is very useful for writing windows paths:
1
2
print r'C:\afolder\afile.txt'
C:\afolder\afile.txt
One thing to keep in mind when using raw strings is that while quotes can be escaped, the backslash you use to escape them will be showed in the final string:
1
2
print r'don\'t say I didn\'t tell you'
don\'t say I didn\'t tell you
You can also use raw triple-quoted strings:
1
2
3
4
5
6
print r'''"I have quotes (')",
also slashes \
and I am multi-lined'''
"I have quotes (')",
also slashes \
and I am multi-lined
Unicode Strings
As for python 3.0 all strings will be Unicode strings. For versions before that you have to add u before the string:
1
2
print u'I am unicode á'
I am unicode á
programming
python
]