To make a string that prints exactly as you type it, use triple quotes, either single (""") or
double ("""). Python prints the string with line breaks and spaces exactly as you enter
them, as shown:
>>> spam = """
... spam spam spam spam spam ... and spam
... """
>>> print spam
spam spam spam spam spam and spam
Triple quotes are most commonly used to create docstrings. A docstring is a short description of a code block, such as a function. Python's help function automatically formats and returns the text of the docstring when you ask for help on the code block. For more about docstrings, see Chapter 11.
Tip Triple quotes are also useful for surrounding text that has both single and double quotation marks.
Ways to escape
To tell Python to give special treatment to a character in a string literal, precede it with a backslash character (\), also called the escape character.
Table 6-1 lists some of the characters that do special things when preceded by a backslash. (These are commonly called escape codes.)
Table 6-1: Some Python Escape Codes Open table as spreadsheet
Character Meaning
\ as last character on a line Text on the next line goes with the text on this line.
\\ Backslash \' Single quote \" Double quote \e Escape key \n Linefeed \t Tab
\0 nn Octal character (nn is a two-digit number)
\x nn Hexadecimal character (nn is a two-digit number)
One way to use the backslash is to tell Python to treat a quotation mark as a regular character rather than as a "string starter or ender," as in this example:
>>> whiteRabbit = 'Where\'s my pocket-watch?' >>> print whiteRabbit
Where's my pocket-watch?
Tip It's easier to read strings that don't have escape characters within the code, so avoid them if you can. If you have text with both single and double quotes, consider using triple quotes to designate it as a string.
To tell Python that a backslash is part of your string and not an escape character, precede it with a second backslash, as shown:
>>> path = "C:\\Applications" >>> print path
C:\Applications
Raw strings
To tell Python not to look for any escape codes in a string, specify the string as a raw string. You might want to do this when handling Windows pathnames, which include the backslash character.
Tip Raw strings also simplify regular expression searches, which also use backslashes as special characters. See Chapter 18.
>>> path = r"C:\Applications" >>> print path
C:\Applications
Warning There's one case in which raw strings aren't completely raw. It's an error to end a raw string with a backslash because Python thinks you're using the backslash to escape the quote mark that ends the string. To type a string that ends with a backslash, you must use a regular string. You can add it to the raw string simply by typing it on the same line. If you type several strings on the same line, you don't need to use a concatenation operator:
>>> print r'C:\data\myprogams' '\\' C:\data\myprograms\
Being wordy
There are several ways to create a string that will span more than one line.
• To write a string that will print exact as you entered it, surround the string with triple-quotes (""" or """).
• To write text that will print on several lines, add the linefeed escape code \n
where you want a new line to start, like this:
• >>> cheese = "cheeses available:\nsavoyard\nboursin"
• >>> print cheese
• cheeses available:
• savoyard
• boursin
• To turn two strings on two lines into a single string, surround them with parentheses. We recommend this method for entering long strings because it's easy to read. For example:
• >>> x = ("hello"
• ... "world")
• >>> print x
• helloworld
• To create a long string, type a backslash character (\) at the end of a line. What
you type after you press Return counts as part of the same line.
• >>> longline = "this is a very long line a very long \
• ... line a very long line"
• >>> print longline
• this is a very long line a very long line a very long line
Warning The end-of-line backslash doesn't work if you add spaces after the backslash.
How a string looks inside Python
To see how Python internally represents a string, type the name of the string and press Return. In this example, Python represents a newline character with the escape character
\n:
... i saw a spam that wasn't >>> modernlife
"as i was surfing on the air\ni saw a spam that
When you ask Python to print a string, it interprets the newline character. So the
modernlife string is printed on two lines, like this: >>> print modernlife
as i was surfing on the air i saw a spam that wasn't there
"Please repeat": String operators
Several of Python's operators work with strings. This section covers the following operators:
• Concatenation (+): Sticks strings together.
• Repeating (*): Copies a string several times.
• Size testing (<, >): Determines which of two strings is bigger or smaller.