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.
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
Use triple quotes (''' or """) to write long text blocks that preserve literal newlines.
- 2
Wrap consecutive strings in parentheses ( ) without commas to split code visually without adding newlines to the output.
- 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.
# 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)--- 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.
Python Strings
Master string manipulation in Python. Learn string methods, slicing, concatenation, and formatting techniques with clear, executable code examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.