Skip to main content

How to Split a Long String Over Several Lines in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A long message makes a line of code too wide to read. Python joins strings written right next to each other into one, so you can break the message into pieces inside brackets, one per line, and still get a single string. This guide shows how, with real output from Python 3.15.2.

Syntax​

Syntax
<name> = (
"<first piece> "
"<second piece>"
)
PartWhat it means
( ... )Brackets let the expression continue over several lines
"<first piece> " "<second piece>"Strings with only spaces or line breaks between them become one string
The space at the end of a piecePython adds nothing between the pieces, so the spaces are yours to place

Python's language reference: "Multiple adjacent string or bytes literals, possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation". It adds: "This feature is defined at the syntactical level, so it only works with literals."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

long_string.py
greeting = (
"Welcome to the shop. "
"Ask about shipping, returns or refunds."
)
print(greeting)

print("snow" "board")

The long_string.py script in a macOS-style code window: greeting is two strings inside brackets on separate lines, then print of the two words snow and board written side by side.

Step 2: Run It​

Terminal
uv run python long_string.py
Output
Welcome to the shop. Ask about shipping, returns or refunds.
snowboard

The output in a terminal: Welcome to the shop. Ask about shipping, returns or refunds. on one line, then snowboard.

The two pieces of greeting became one line. "snow" "board" shows that Python adds nothing between the pieces: the space in the greeting comes from the end of "Welcome to the shop. ".

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the space at the end of a piece"Welcome to the shop." "Ask about shipping." prints Welcome to the shop.Ask about shipping.End each piece with the space it needs
Trying it with a variable, as in a "y"SyntaxError: invalid syntax: it only works with literalsUse + or an f-string for variables
Forgetting a comma between list items["tent" "stove"] is one item, "tentstove"Put commas between list items

Troubleshooting​

SymptomLikely causeFix
A list has fewer items than writtenTwo strings without a comma were joinedAdd the missing comma

FAQ​

Do I need the brackets?​

Only when the pieces are on different lines. On one line, "snow" "board" works without them.

Is this better than +?​

For literals, yes: Python joins them once, while reading the code. + joins at run time and is needed for variables.

Conclusion​

Two strings written one under the other inside brackets printed as one sentence, and "snow" "board" printed snowboard, with no space added between the pieces.

Additional Resources​