Concepts / Parsing XML with ElementTree

Parsing XML with ElementTree

XML is structured across multiple lines, but an ordinary Python string requires escape sequences such as \n to represent line breaks.

  • Programming

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.

rootopening XML linerootline in stringitemindented XML lineitemnewline plus indentationpreservedrootclosing XML linerootline in stringbecomesbecomesbecomes
What part of the code's indentation and line breaks is preserved inside the resulting string?
Key point

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

Worked example

Writing XML with triple single quotes

Represent a small formatted XML document as a Python string without writing explicit newline escape sequences.

  1. 1
    Open the string

    Use three single quotes before the first XML line.

  2. 2
    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.

  3. 3
    Close the string

    Use three matching single quotes after the final XML line.

Result

The variable contains a multi-line string whose XML line breaks and indentation are preserved.

python
Output
<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.

''' XML '''triple single quotesmulti-line stringXML formatting preserved""" XML """triple double quotesmulti-line stringXML formatting preservedcreatescreates
What is different, and what stays the same, when the same XML is enclosed in triple single quotes versus triple double quotes?
python

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.

XML textmultiple linestriple quotesopening and closingdelimitersPython stringline breaks and spacespreservedXML datareadable representationplace betweencreatesrepresents
How does formatted XML become a readable Python value that can be used as XML data?
In the real world

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

  • Common mistake

    Treating indentation as decoration that Python will discard

    What it looks like
    xml_text = '''<catalog>
        <book>Python</book>
    </catalog>'''
    Why it fails

    The spaces before the book element are inside the triple-quoted string, so they are part of its value.

    Do this instead

    Inspect the layout between the opening and closing triple quotes as actual string content.

  • Common mistake

    Using explicit newline escapes when the XML should remain readable in the code

    What it looks like
    xml_text = '<catalog>\n    <book>Python</book>\n</catalog>'
    Why it fails

    This represents the line breaks, but the XML structure is less visible because the code contains escape sequences instead of visibly separated lines.

    Do this instead

    Use a triple-quoted string when preserving and displaying the XML layout directly is useful.

  • Common mistake

    Assuming triple single quotes and triple double quotes have different multi-line behavior

    What it looks like
    xml_a = '''<catalog></catalog>'''
    xml_b = """<catalog></catalog>"""
    Why it fails

    Both forms create multi-line strings and preserve the same kinds of formatting.

    Do this instead

    Choose the quote style based on readability, consistency, or convenience.

  • Common mistake

    Forgetting the closing triple-quote delimiter

    What it looks like
    xml_text = '''<catalog>
        <book>Python</book>
    </catalog>
    Why it fails

    Python needs matching opening and closing triple quotes to determine the complete string.

    Do this instead

    Close the string with the same triple-quote style used to open it.

Practice the Representation

EASY

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?

  • Yes, the spaces are preserved

  • No, Python removes indentation automatically

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

  1. 1
    Triple single quotes and triple double quotes both create multi-line Python strings.
  2. 2
    Line breaks, indentation, and spaces between the delimiters become part of the string value.
  3. 3
    Triple-quoted strings keep formatted XML readable in Python code without requiring explicit newline escape sequences.
  4. 4
    The choice between triple single quotes and triple double quotes is a matter of style and convenience.
  5. 5
    Always treat the visual layout inside the delimiters as possible string content.

At a glance

  • 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.

Related concepts

More concepts starting with P