XML Structure and Syntax
Triple single quotes (''') and triple double quotes (""") both create multi-line strings in Python.
Readable XML in Python
XML data is structured across multiple lines, but representing that structure with an ordinary Python string can make the code difficult to read because line breaks must be written with escape sequences such as \n. Triple-quoted strings provide a more readable alternative: you can write the multi-line XML layout directly in your Python code.
The central idea is simple: the text between an opening and closing set of triple quotes becomes one string value, while preserving the line breaks, indentation, and spaces written inside it.
The Delimiter Choice
Python supports two equivalent forms for creating a multi-line string. Triple single quotes use three single-quote characters, and triple double quotes use three double-quote characters. Both forms create multi-line strings and serve the same purpose. The choice is mainly a matter of style and convenience.
In both generated examples, the XML-shaped text is stored as a multi-line Python string. Changing the delimiter from triple single quotes to triple double quotes does not change the basic purpose of the string. The important content is the text enclosed by the delimiters, including its line breaks and spaces.
From Source Layout to String Value
Triple-quoted strings have a direct relationship between how the code looks and what the string contains. Pressing Enter inside the triple-quoted region places a line break in the string value. Adding spaces before text places those spaces in the string value as well. Indentation is therefore not merely visual formatting when it appears between the opening and closing triple quotes.
Mapping an XML layout into one string
Determine which parts of the following triple-quoted value are preserved in the string.
Opening delimiter: The opening triple quotes mark the beginning of the multi-line string.
Line breaks: Each line break typed between the delimiters becomes a line break in the string value.
Indentation: The spaces used before the item text are preserved as spaces in the string value.
Closing delimiter: The closing triple quotes mark the end of the string.
The resulting value is one string containing the XML-shaped text together with the line breaks and indentation shown in the source layout.
XML Representation Uses
Triple-quoted strings are especially useful when XML must be stored or represented as text in Python. The XML can remain visibly multi-line and indented in the code instead of being compressed into one line or written with explicit line-break escape sequences. This makes the XML structure easier to see and helps avoid missing or incorrect escape sequences.
- Representing XML data for testing
- Embedding configuration data
- Preparing payloads to send across the web
The generated example keeps the XML-shaped structure visible in the Python source. The line arrangement and indentation inside the delimiters are part of the resulting string value, which is the main reason this representation is convenient for multi-line XML data.
Mistakes with Triple Quotes
Assuming indentation inside the delimiters is ignored
Spaces inside the opening and closing triple quotes become part of the string value.
Fix:
Place indentation deliberately, because it is preserved in the multi-line string.Assuming line breaks are only visual
The line break becomes a line break in the string value.
Fix:
Review the layout inside the delimiters as part of the actual text you are creating.Thinking triple single quotes and triple double quotes have different multi-line behavior
Both delimiter styles create multi-line strings and serve the same purpose.
Fix:
Choose between them according to style and convenience.Using ordinary quoted strings without considering readability
The XML structure can become difficult to read and less visible in the code.
Fix:
Use a triple-quoted string when preserving a readable multi-line XML layout is useful.
Practice the Mapping
Suppose a triple-quoted string contains three XML-shaped lines. Before checking your answer, identify which elements of the source layout will appear in the string value: the opening and closing delimiters, the line breaks, the indentation spaces, or all of these.
Hints
- Focus on everything located between the opening and closing triple quotes.
- Recall what happens when you press Enter or add spaces inside that region.
- Separate the delimiters from the content they enclose.
What do you think happens?
Which parts of the multi-line layout become part of the string value?
Reveal answer
Answer: Line breaks, indentation, and spaces inside the delimiters
The source layout inside the triple quotes directly contributes to the string value. The triple quotes mark the boundary, while the enclosed line breaks and spaces are preserved.
Essential Takeaways
- Triple single quotes and triple double quotes both create multi-line Python strings.
- The choice between the two delimiter styles is a matter of style and convenience.
- Line breaks, indentation, and spaces inside the delimiters become part of the string value.
- Triple-quoted strings make multi-line XML data easier to read because the XML structure can be written directly in the code.
- Use triple-quoted strings when representing XML for purposes such as testing, embedding configuration data, or preparing web payloads.
Key Takeaways
- Triple-quoted strings let Python represent text across multiple lines.
- Triple single quotes and triple double quotes provide equivalent multi-line string behavior.
- Every line break, indentation space, and other space inside the delimiters is preserved in the string value.
- This direct preservation makes triple-quoted strings suitable for representing readable, multi-line XML data.