Multiline Strings

Concepts covered: Type Conversion

Some text runs across several lines: an email body, a receipt, the help message of a command. A string can span as many lines as you like if you open and close it with 3 quotes in a row. Both """ and ''' work, and every line break you type between the opening and closing quotes is stored as part of the string. The string ends only at the matching 3 quotes.

message = """Your order has shipped.
Expected delivery: Thursday.
Thanks for shopping with us."""
print(message)

Output:

Your order has shipped.
Expected delivery: Thursday.
Thanks for shopping with us.

One print call produced 3 lines because the string itself contains a line break after shipped. and another after Thursday.. Nothing special happened at print time: print wrote the characters it was given, and 2 of those characters moved the cursor to the next line.

A line break is a character

The line break is stored as a single character, so it counts when you walk the string with a loop. The string below shows 4 visible letters on 2 lines, and the loop counts 5.

note = """ab
cd"""
count = 0
for ch in note:
    count = count + 1
print(count)

Output:

5

The string starts right after the quotes

Pressing Enter straight after the opening """ puts a line break at the very start of the string, so the first thing printed is an empty line. This is the most common surprise with triple quotes. The program below prints a blank line before Welcome, and done shows where the string ended.

banner = """
Welcome"""
print(banner)
print("done")

Output:


Welcome
done

To begin with Welcome on the first printed line, type it on the same line as the opening quotes, as the shipping message above does. Likewise, a line break before the closing quotes adds a trailing blank line to whatever you print.

Indentation inside the quotes is kept

Inside a function body your code is indented, and any spaces you type at the start of a continuation line become spaces in the string. The receipt below looks tidy in the source, but the second printed line starts with 4 spaces that came from the indentation.

def receipt():
    text = """Total: 40
    Paid: 40"""
    return text

print(receipt())

Output:

Total: 40
    Paid: 40

The content of a string is text rather than code, so Python does not require it to be indented. Writing Paid: 40 flush against the left margin, inside the function, removes the leading spaces while the rest of the function stays indented as usual. It looks odd in the editor and prints correctly.

You have met triple quotes already: a docstring is a triple-quoted string written as the first statement of a function, and it keeps the line breaks you type like any other. Its later lines can still sit at the function body's indentation, because documentation tools strip the indentation those lines share before showing the text, and Python itself does the same when it stores a docstring from version 3.13 onward. An ordinary triple-quoted string such as the receipt above gets no such treatment, so its leading spaces stay.

Multiline strings in pipeline code

Pipelines keep plenty of text that has a shape worth preserving: a query sent to a database, a template for an alert email, the help text of a command. A triple-quoted string lets the shape in the source match the shape of the output, so a reviewer sees the query laid out as it will run.

query = """SELECT
    user_id,
    minutes
FROM listens"""
print(query)

Output:

SELECT
    user_id,
    minutes
FROM listens
One pair of quotes
  • Starts and ends on the same source line
  • A line break before the closing quote is a syntax error
  • Fits anything that stays on one line, like a name or a sentence
Three quotes in a row
  • Can run across many source lines
  • Every typed line break and leading space is stored
  • Fits docstrings and any message or query that runs past one line

How many characters does the loop count?

text = """
Hi"""
count = 0
for ch in text:
    count = count + 1
print(count)

Returning a multiline message

A function can build a multiline string and hand it back with return, and the caller prints it like any other value. The same rules apply inside the function: the text starts with whatever follows the opening quotes, so the first line of the message goes right after them. Continuation lines sit at the left margin, because spaces typed before them would be stored.

def reminder():
    text = """Meeting at 10
Room 4B"""
    return text

print(reminder())
print("end")

Output:

Meeting at 10
Room 4B
end

The output has exactly 2 lines of message and no blank line above them, because Meeting at 10 begins on the line of the opening quotes and Room 4B begins at column 0. Move Meeting at 10 down a line and the printed message would open with an empty line.

About this lesson

This section is part of the Strings: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.