How to Write Multiline Strings in Python (Triple Quotes & Joins)

Learn the best ways to write multiline strings in Python. Explore triple quotes, parentheses concatenation, newline join methods, and format controls.

Try Code in Editor

Explanation

When writing text blocks inside your application—such as SQL queries, HTML templates, CLI instructions, or lengthy debug messages—you will often need to span string values across multiple lines. Python offers several ways to define multiline strings, each with distinct formatting implications.

The most direct and common way to write a multiline string is using triple quote syntax (either `'''` or `"""`). Triple-quoted strings preserve all character inputs literally, including raw newlines and indentation. This makes formatting highly visual but has a drawback: any leading tabs or spaces used to align the code inside nested loops or classes will become part of the actual string value.

To define a multiline string that visually spans multiple lines in your source code but is compiled into a single long line (or doesn't capture code indentation whitespace), you can wrap standard single-quoted strings in parentheses. Python automatically concatenates adjacent string literals inside parentheses, making your code clean and highly readable.

Step-by-Step Implementation

  1. 1

    Use triple quotes (''' or """) to write long text blocks that preserve literal newlines.

  2. 2

    Wrap consecutive strings in parentheses ( ) without commas to split code visually without adding newlines to the output.

  3. 3

    Use textwrap.dedent() when working with triple quotes inside classes or functions to clean up leading indentation.

Code Example

This script demonstrates writing multiline text using triple quotes, parentheses string concatenation, and clean indents.

multiline_strings.py
Try in Editor
# 1. Triple Quote Syntax (preserves all newlines and indentation)
sql_query = """SELECT id, username, email
FROM users
WHERE active = True
ORDER BY id DESC;"""

print("--- Triple Quotes Query ---")
print(sql_query)

# 2. Parentheses Concatenation (splits code line, compiles into single line)
long_message = (
    "This is a long message that we split "
    "across multiple lines in our editor, "
    "but it compiles into a single text block."
)
print("\n--- Parentheses Concatenation ---")
print(long_message)

# 3. Joining list elements for clean line breaks
lines = [
    "First instruction line.",
    "Second instruction line.",
    "Third instruction line."
]
joined_text = "\n".join(lines)
print("\n--- Joined Lines ---")
print(joined_text)
Terminal Output
--- Triple Quotes Query ---
SELECT id, username, email
FROM users
WHERE active = True
ORDER BY id DESC;

--- Parentheses Concatenation ---
This is a long message that we split across multiple lines in our editor, but it compiles into a single text block.

--- Joined Lines ---
First instruction line.
Second instruction line.
Third instruction line.

Frequently Asked Questions

How do I remove leading indentation from a nested triple-quoted string?

Import the built-in textwrap module and pass the string to textwrap.dedent(text) to clean up leading spaces.

What is the difference between single and double triple quotes?

There is no functional difference between ''' and """. Choose one convention and stick to it consistently across your project.

Related How-To Guides

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.