String Escape Sequences in Python
Triple single quotes (''') and triple double quotes (""") both create multi-line strings in Python.
From Delimiters to Value
A triple-quoted string lets a Python string span multiple source-code lines. Python provides two equivalent delimiter styles: triple single quotes, written as three single-quote characters, and triple double quotes, written as three double-quote characters. The characters between the opening and closing delimiters become the string value.
What do you think happens?
If a triple-quoted string is written across three source-code lines, what happens to those line breaks?
Reveal answer
Answer: The line breaks become part of the resulting string value.
Triple-quoted strings preserve the line breaks, indentation, and spaces that appear between their opening and closing delimiters.
Triple-Quote Syntax
The opening triple quotes mark where the string begins, and the closing triple quotes mark where it ends. The delimiters are not the content you are trying to store. The content is everything between them, including ordinary characters, line breaks, indentation, and spaces.
Reading the Delimiters
Identify which characters belong to the value in the triple-quoted string.
Find the opening delimiter: The first three single quotes mark the beginning of the string.
Read the content: First line, the line break, and Second line are inside the delimiters, so they belong to the string value.
Find the closing delimiter: The final three single quotes mark the end of the string and are not part of its content.
The value contains two lines: First line followed by a line break and then Second line.
Two Equivalent Delimiter Styles
Triple single quotes and triple double quotes serve the same purpose: both create multi-line strings. Choosing one rather than the other is a matter of style and convenience. The delimiter style does not create a different kind of string value.
| Delimiter style | Purpose | Result |
|---|---|---|
| Triple single quotes | Create a multi-line string | A multi-line string value |
| Triple double quotes | Create a multi-line string | A multi-line string value |
Formatting Becomes Content
A triple-quoted string maps the visual layout inside its delimiters directly into the string value. Pressing Enter in the source creates a line break in the value. Indenting a line or placing spaces between words puts those spaces into the value as well. This differs from an ordinary single- or double-quoted string, where a newline must be represented explicitly with an escape sequence such as backslash-n.
XML as a Multi-Line Value
XML is structured across multiple lines, often using indentation to make its nested structure readable. Representing XML in an ordinary string can require escape sequences such as backslash-n for line breaks, which makes the XML structure harder to see in the Python source. A triple-quoted string allows the XML text to be written in its multi-line, indented form, preserving that formatting directly in the string value.
Representing Structured XML
Explain why the XML example is easier to read as a triple-quoted string than as one ordinary string containing explicit newline escape sequences.
Identify the structure: The XML has separate lines and indentation that show the relationship between the catalog and item elements.
Use triple quotes: The XML can be typed across multiple source-code lines between triple double quotes.
Map layout to value: The line breaks and indentation typed around the XML become part of the string value.
The Python source visibly preserves the XML's multi-line structure, making the represented XML easier to read.
Triple-quoted strings are appropriate when XML must be stored or represented in Python, including situations such as testing XML, embedding configuration data, or preparing a payload to send across the web.
Mistakes with Preserved Whitespace
Assuming indentation is only for Python readability
Spaces inside the delimiters become part of the string value.
Fix:
Place the text where you want it in the resulting value, remembering that source indentation is preserved.Assuming source-code line breaks disappear
Line breaks between the opening and closing triple quotes become line breaks in the string.
Fix:
Use triple-quoted strings when those line breaks are wanted; otherwise, account for the preserved layout.Treating triple single quotes and triple double quotes as different string types
Both styles serve the same purpose.
Fix:
Choose between them based on style and convenience.
Before using a triple-quoted string for XML, inspect the source layout carefully. Check where the first and last line breaks occur and whether indentation is intended to be part of the XML text.
Check Your Understanding
Write a triple-quoted string using triple single quotes that contains two lines of XML-like text. Then identify every line break and indentation space that belongs to the resulting string value.
Hints
- Place the opening and closing triple single quotes around both lines.
- Treat every Enter key press between the delimiters as a line break in the value.
- Treat every space typed before a line as part of that line's value.
Compare triple single quotes with triple double quotes. Explain what changes when you switch the delimiters and what does not change about the resulting string.
Hints
- The delimiter characters look different.
- Both forms create multi-line strings.
- The choice is a matter of style and convenience.
Key Takeaways
- Triple single quotes and triple double quotes both create multi-line strings.
- Everything between the opening and closing triple quotes becomes part of the string value.
- Line breaks, indentation, and spaces in the source code are preserved in the string.
- Triple-quoted strings make multi-line XML easier to represent because the XML structure remains visible in the Python source.
- The choice between triple single and triple double quotes is based on style and convenience.
Key Takeaways
- Triple-quoted strings can span multiple lines.
- Triple single quotes and triple double quotes have the same purpose.
- Source-code line breaks, indentation, and spaces inside the delimiters become string content.
- This preservation makes triple-quoted strings useful for representing structured XML data.
- Always inspect the layout inside the delimiters because visual formatting affects the resulting value.