Parsing XML with ElementTree
Triple single quotes (''') and triple double quotes (""") both create multi-line strings in Python.
Readable XML in Python
XML is structured across multiple lines, but an ordinary Python string requires escape sequences such as \n to represent line breaks. Those escape sequences can make the code harder to read and hide the XML structure. Triple-quoted strings provide a way to write multi-line XML directly in Python code while preserving the line breaks and indentation you type.
How Formatting Becomes Content
A triple-quoted string begins with either three single quotes or three double quotes and ends with the matching three quotes. Everything between those boundaries becomes part of the string value. If the text spans several lines, the line breaks become newline characters. If a line contains indentation or other spaces, those spaces are preserved as well. The layout you see inside the quotes is therefore not merely visual code formatting; it contributes to the actual string.
Pressing Enter inside the triple-quoted string adds a newline to the value. Typing spaces before text adds those spaces to the value. Do not assume that indentation is automatically removed.
A Multi-Line XML Value
Writing XML with triple single quotes
Represent a small formatted XML document as a Python string without writing explicit newline escape sequences.
Open the string: Use three single quotes before the first XML line.
Write the XML: Place each XML line on its own line. The line breaks and spaces typed between the opening and closing quotes become part of the string.
Close the string: Use three matching single quotes after the final XML line.
The variable contains a multi-line string whose XML line breaks and indentation are preserved.
<catalog>
<book>Python</book>
</catalog>The value contains three XML lines. The second line begins with four spaces because those spaces were written between the triple quotes. The displayed XML structure is therefore also represented in the string itself.
Two Equivalent Quote Styles
Triple single quotes and triple double quotes both create multi-line strings. They preserve line breaks, indentation, and spaces in the same way. Choosing one over the other is a matter of style and convenience; neither form provides a different kind of multi-line string.
From XML Text to Python Data
When XML is represented in Python, the process begins with XML text and produces a Python string value containing that text. Triple quotes make this representation readable because the XML remains visibly arranged across lines rather than being flattened into one long line or filled with explicit newline escape sequences. This is useful when storing XML for testing, embedding configuration data, or preparing a payload to send across the web.
A test can keep a sample XML document in a triple-quoted string so the sample remains visibly structured. The same approach can represent embedded configuration data or prepare XML text as a payload for communication across the web.
Mistakes with Preserved Whitespace
Treating indentation as decoration that Python will discard
The spaces before the book element are inside the triple-quoted string, so they are part of its value.
Fix:
Inspect the layout between the opening and closing triple quotes as actual string content.Using explicit newline escapes when the XML should remain readable in the code
This represents the line breaks, but the XML structure is less visible because the code contains escape sequences instead of visibly separated lines.
Fix:
Use a triple-quoted string when preserving and displaying the XML layout directly is useful.Assuming triple single quotes and triple double quotes have different multi-line behavior
Both forms create multi-line strings and preserve the same kinds of formatting.
Fix:
Choose the quote style based on readability, consistency, or convenience.Forgetting the closing triple-quote delimiter
Python needs matching opening and closing triple quotes to determine the complete string.
Fix:
Close the string with the same triple-quote style used to open it.
Practice the Representation
Write two Python variables containing the same two-line XML text. Use triple single quotes for the first variable and triple double quotes for the second. Give the second XML line visible indentation, then explain which parts of that indentation and line break become part of each string.
Hints
- Use three matching quote characters at both ends of each string.
- Place the XML lines on separate lines inside the delimiters.
- Remember that spaces typed before the second XML line are preserved.
What do you think happens?
If an XML line is indented with four spaces inside a triple-quoted string, are those spaces part of the resulting string?
Reveal answer
Answer: Yes, the spaces are preserved.
Everything between the opening and closing triple quotes, including indentation, spaces, and line breaks, becomes part of the string value.
Key Takeaways
- Triple single quotes and triple double quotes both create multi-line Python strings.
- Line breaks, indentation, and spaces between the delimiters become part of the string value.
- Triple-quoted strings keep formatted XML readable in Python code without requiring explicit newline escape sequences.
- The choice between triple single quotes and triple double quotes is a matter of style and convenience.
- Always treat the visual layout inside the delimiters as possible string content.
Key Takeaways
- Triple-quoted strings are Python strings delimited by either three single quotes or three double quotes.
- Every line break, indentation character, and space inside the delimiters is preserved in the string value.
- This makes triple-quoted strings suitable for representing formatted XML directly in Python code.
- Triple single quotes and triple double quotes serve the same purpose; select between them for style and convenience.