Concepts / Sending XML Data Over HTTP

Sending XML Data Over HTTP

Triple single quotes (''') and triple double quotes (""") both create multi-line strings in Python.

  • Programming

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.

python
startscontinuescontinuesends at'''opening boundary<message>string content<text>Hello</text>string content</message>string content'''closing boundary
What do the opening and closing triple quotes contain, and how do they define the boundaries of the string?

Two Equivalent Styles

Writing the Same XML Two Ways

Represent the same multi-line XML content using triple single quotes and triple double quotes.

Triple single quotes: The XML is placed between three single quotes at the beginning and three single quotes at the end.

Triple double quotes: The same XML is placed between three double quotes at the beginning and three double quotes at the end.

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

python

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.

becomesSource layoutline breaks and spacestyped between quotesString valuematching line breaks andspaces
Which line breaks and spaces from the Python code become part of the string value?

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.

representsis prepared asPythontriple-quotedstringreadable XML textXML dataline breaks and indentationpreservedHTTP payloaddata prepared for sendingacross the web
How does XML move from a Python multi-line string into data prepared for an HTTP request?

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

  • Assuming indentation is ignored

    The spaces before the text element are inside the triple-quoted boundaries, so they become part of the string value.

    Fix: Treat every space and line break between the opening and closing triple quotes as content.

  • Expecting triple quotes to send the XML automatically

    Triple quotes create a multi-line string; they do not themselves perform the web transfer.

    Fix: Use the resulting string as XML data when preparing a payload to send across the web.

  • Thinking triple single quotes and triple double quotes have different multi-line behavior

    Both quote styles create multi-line strings and preserve the content between their delimiters.

    Fix: Choose the style that is more convenient or consistent with the surrounding code.

  • Treating code indentation outside the string as automatically part of the value

    The important boundary is what appears between the opening and closing triple quotes. Spaces outside those boundaries are not part of the string content.

    Fix: Inspect the text inside the delimiters when deciding which spaces and line breaks belong to the XML string.

Practice the Mapping

EASY

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?

  • Yes
  • No
Reveal answer

Answer: Yes

Line breaks between the triple-quote delimiters are preserved as newline characters in the multi-line string.

Key Takeaways

  1. Triple single quotes and triple double quotes both create multi-line Python strings.
  2. Everything between the opening and closing triple quotes, including line breaks, indentation, and spaces, becomes part of the string value.
  3. Triple-quoted strings make XML easier to read because the XML structure can be written directly across multiple lines.
  4. The choice between triple single quotes and triple double quotes is a style and convenience decision.
  5. The resulting string represents the XML data; it can then be prepared as a payload to send across the web.

Key Takeaways

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