Strings¶
Representing text in a computer is accomplished with a sequence of numbers. Internally this is a structure that packs the numbers all together efficiently (called an array). In Python strings are “immutable” (un-changeable) so once you create the string you can’t change it. Other languages have “mutable” strings meaning they can be modified once they are created.
>>> "Hello world"
'Hello world'
>>> "234"
'234'
>>> "234" == 234
False
>>> '234' == "234"
True
>>> ' 234' == '234'
False
Strings are a different type of thing than numbers, and they won’t compare equal to them. In many languages comparing two objects of a different type will raise an error rather than just returning False.
Strings can have “escaped characters” in them. These are characters which would be difficult to type or which we don’t want to type because it’s hard to tell what they are when looking at the code (such as a tab or newline).
>>> "\\"
'\\'
>>> print('\\')
\
>>> "Hello\nWorld"
'Hello\nWorld'
>>> print("Hello\nWorld")
Hello
World
>>> print('Hello\tWorld')
Hello World
>>> print('\u00e9')
é
A special case is the quote character that started the string. It is typed as the back-slash character and the quote character. Because Python doesn’t care about which type of quotes you use for a string, you can often just swap the wrapper string to get the same effect as escaping the quote character. However, if your string happens to have both quote types, or you want to include characters such as newline (n) with an actual new line, you can use triple-quoted strings (using either “ or ‘).
>>> "\"this\""
'"this"'
>>> '"this"'
'"this"'
>>> print("He said \"I can't!\"\nHe left some time later")
'"\''
>>> print("He said \"I can't!\"\nHe left some time later")
He said "I can't!"
He left some time later
>>> print('''He said "I can't"
... He left some time later''')
He said "I can't"
He left some time later
Note
Escaping can be thought of as an escape from the normal interpretation of the string. If you go on to program computer professionally you will find that almost every format and langauge you process has some sort of escape system to allow for embedding “special” things.