Triple-Quoted Strings
You can specify multi-line strings using triple quotes - ( """ or ''' ). You can use single quotes and double quotes freely within the triple quotes. An example is:
Reading String Boundaries
A string is surrounded by delimiters that tell Python where its contents begin and end. With ordinary strings, the delimiter is usually a single quote or a double quote. Triple-quoted strings use three single quotes or three double quotes as the delimiter, so the string can extend across multiple lines and can contain single and double quote characters freely.
Triple Quotes and Direct Line Breaks
Triple quotes are useful when the string itself needs to contain multiple lines. Python allows either three double quotes or three single quotes as the delimiter. Inside the triple-quoted string, single quotes and double quotes can appear freely without being escaped, provided they do not form the closing triple-quote delimiter.
She said, 'Hello!'
Then she left.Escape Sequences as Character Instructions
An escape sequence is a two-character combination consisting of a backslash and another character. It tells Python to interpret the second character specially rather than literally.
| Sequence | Meaning | Use |
|---|---|---|
| \' | Single quote | Place a single quote inside a string delimited by single quotes |
| \" | Double quote | Place a double quote inside a string delimited by double quotes |
| \\ | Backslash | Place a literal backslash in the string |
| \n | Newline | Insert a line break into the string value |
| \t | Horizontal tab | Insert a tab character into the string value |
Common escape sequences
The two characters you type in the source code are not always the two characters stored in the resulting string. For example, \n appears as a backslash followed by n in the source code, but it represents one newline character in the string value. Likewise, \\ in the source code produces one literal backslash.
Choosing Quotes and Escaping Delimiters
Keeping an Apostrophe Inside a String
Represent the text What's your name? as a Python string.
Use the same quote type: If single quotes delimit the string, the apostrophe in What's must be escaped so Python does not treat it as the closing delimiter.
Write the escaped form: The inner apostrophe is represented with the single-quote escape sequence.
Use a different delimiter when convenient: Double quotes can delimit the string instead, allowing the apostrophe to appear without an escape.
\'What's your name?\' or "What's your name?"
What do you think happens?
What happens when Python reads 'What's your name?' without escaping the apostrophe?
Reveal answer
Answer: It sees the apostrophe after What as the end of the string.
The remaining characters are then left unparseable, so the expression causes a syntax error.
Joining Source Lines Without Newlines
A backslash at the very end of a source line can continue a string onto the next source line. The backslash must be the final character on that line, with no spaces or other characters after it. This form of continuation does not add a newline to the string value, so the resulting value remains one line.
This is the first sentence. This is the second sentence.Mistakes with Delimiters and Backslashes
Putting an unescaped quote inside a string delimited by the same quote type
Python treats the apostrophe after What as the closing delimiter, leaving the rest of the text outside a valid string.
Fix:
Escape the apostrophe as \' or use double quotes around the complete string.Using a single backslash when the string must contain a literal backslash
Python interprets a backslash together with the following character as an escape sequence.
Fix:
Use \\ in the source code to produce one literal backslash.Expecting \n to remain visible as two characters when the string is printed
The escape sequence represents a newline character in the string value.
Fix:
Remember that source spelling and stored string content can differ.Adding spaces after a line-continuation backslash
The continuation backslash must be the final character on the line.
Fix:
Place the backslash at the line end with no trailing characters.
First decide whether the string needs actual line breaks. Use triple quotes when the string should contain multiple lines directly. Use \n when the line break should be represented inside an otherwise ordinary quoted string. Use a final backslash on a source line only when you want to format a long string across source lines without inserting a newline into its value.
Apply the Rules
For each goal, choose the appropriate technique: triple quotes, an escape sequence, or a line-continuation backslash. Goal 1: include an apostrophe inside a single-quoted string. Goal 2: put two pieces of text on separate lines in the resulting value. Goal 3: split a long source line across two physical lines while keeping the resulting value on one line. Goal 4: include a literal backslash character.
Hints
- A quote matching the delimiter needs escaping.
- The newline escape sequence inserts a line break into the value.
- A backslash at the very end of the source line joins lines without adding a newline.
- A double backslash produces one literal backslash.
- Triple-quoted strings use three single quotes or three double quotes and can contain multiple lines. Single and double quotes can appear freely inside them. Escape sequences begin with a backslash and allow strings to contain matching quote characters, literal backslashes, newlines, and tabs. A backslash at the end of a source line joins the next source line without adding a newline to the string value. When debugging, inspect which character Python believes ends the string and whether each backslash is acting as an escape sequence or as line continuation.
Key Takeaways
- Triple quotes allow strings to span multiple lines and to contain single and double quotes without escaping those individual quote types.
- Escape sequences use a backslash plus another character to represent quotes, backslashes, newlines, and tabs.
- The sequence \n adds a newline to the string value, while a backslash at the end of a source line joins lines without adding a newline.
- A quote matching the string delimiter must be escaped or the string must use the other quote type as its delimiter.
- A literal backslash requires two backslashes in the source code.