Sending XML Data Over HTTP
XML is structured across multiple lines, with nested elements often separated by indentation.
Readable XML Payloads
XML is structured across multiple lines, with nested elements often separated by indentation. Representing that structure in an ordinary Python string can be awkward because line breaks must be written with escape sequences such as \n. Triple-quoted strings provide a more readable way to represent XML in Python before preparing it as a payload to send across the web.
Triple single quotes and triple double quotes both create multi-line strings in Python. They serve the same purpose; the choice between them is mainly a matter of style and convenience.
Quote Boundaries
A triple-quoted string begins with three consecutive single quotes or three consecutive double quotes and ends with a matching set of three quotes. Everything between those boundaries belongs to the string value. Because the opening and closing quotes can surround several lines, the string can contain line breaks, indentation, and spaces directly.
Two Equivalent Styles
Writing the Same XML Two Ways
Represent the same multi-line XML content using triple single quotes and triple double quotes.
- 1Triple single quotes
The XML is placed between three single quotes at the beginning and three single quotes at the end.
- 2Triple double quotes
The same XML is placed between three double quotes at the beginning and three double quotes at the end.
- 3Compare the content
The quote style changes, but the XML lines, line breaks, indentation, and spaces inside the boundaries can remain the same.
Both forms create multi-line strings. Neither quote style is inherently better for this purpose.
Formatting Becomes Content
Triple-quoted strings preserve the formatting typed between the delimiters. Pressing Enter in the source code adds a newline character to the string. Adding spaces for indentation adds those spaces to the string as well. This means the visual layout of the triple-quoted portion is not merely decoration: it directly contributes to the resulting string value.
xml_data = '''<message>
<text>Hello</text>
</message>'''The XML is displayed across three source-code lines, with spaces before the text element.
<message>
<text>Hello</text>
</message>The resulting string contains the same line breaks and indentation between its delimiters.
From XML Text to Payload
A triple-quoted string does not perform the web transfer by itself. Its role is to make the XML data readable and preserve its structure while the data is represented in Python. Once the XML has been stored as a string, that string can be used as a payload prepared for sending across the web.
The useful connection is representation: triple quotes keep XML readable in Python, and the resulting string is the XML data that can be prepared as a web payload.
Mistakes with Layout
Common mistake Assuming indentation is ignored
What it looks likexml_data = '''<message> <text>Hello</text> </message>'''Why it failsThe spaces before the text element are inside the triple-quoted boundaries, so they become part of the string value.
Do this insteadTreat every space and line break between the opening and closing triple quotes as content.
Common mistake Expecting triple quotes to send the XML automatically
What it looks likexml_data = '''<message>Hello</message>'''Why it failsTriple quotes create a multi-line string; they do not themselves perform the web transfer.
Do this insteadUse the resulting string as XML data when preparing a payload to send across the web.
Common mistake Thinking triple single quotes and triple double quotes have different multi-line behavior
What it looks likexml_a = '''<message>Hello</message>''' xml_b = """<message>Hello</message>"""Why it failsBoth quote styles create multi-line strings and preserve the content between their delimiters.
Do this insteadChoose the style that is more convenient or consistent with the surrounding code.
Common mistake Treating code indentation outside the string as automatically part of the value
What it looks likexml_data = '''<message> <text>Hello</text> </message>'''Why it failsThe important boundary is what appears between the opening and closing triple quotes. Spaces outside those boundaries are not part of the string content.
Do this insteadInspect the text inside the delimiters when deciding which spaces and line breaks belong to the XML string.
Practice the Mapping
Write one Python variable containing this XML as a triple-single-quoted string and another variable containing it as a triple-double-quoted string. Keep the XML on multiple lines and indent the nested element with spaces. Then identify which line breaks and spaces become part of each string.
Hints
- Put three matching quote characters before and after the XML.
- Use either three single quotes or three double quotes for each variable.
- Count only the spaces and line breaks between the opening and closing delimiters.
What do you think happens?
If a line break is typed between the opening and closing triple quotes, does that line break become part of the string value?
Reveal answer
Yes
Line breaks between the triple-quote delimiters are preserved as newline characters in the multi-line string.
Key Takeaways
- 1Triple single quotes and triple double quotes both create multi-line Python strings.
- 2Everything between the opening and closing triple quotes, including line breaks, indentation, and spaces, becomes part of the string value.
- 3Triple-quoted strings make XML easier to read because the XML structure can be written directly across multiple lines.
- 4The choice between triple single quotes and triple double quotes is a style and convenience decision.
- 5The resulting string represents the XML data; it can then be prepared as a payload to send across the web.
At a glance
- Triple-quoted strings provide a readable way to represent multi-line XML in Python.
- Both triple single quotes and triple double quotes have the same multi-line behavior.
- Line breaks, indentation, and spaces inside the delimiters become part of the string.
- The string preserves XML formatting for use as data prepared for sending across the web.