Skip to main content

How to Continue a String on the Next Line in Python with a Backslash

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A long sentence in a string makes a long line of code. A backslash at the very end of a line, inside the string, tells Python to ignore that line break, so the code wraps while the text stays on one line. This guide shows it in a normal string and in a triple-quoted one, with real output from Python 3.15.2.

Syntax​

Syntax
<name> = "<first part> \
<second part>"
PartWhat it means
\ at the end of a lineInside a string, removes the line break that follows it
<second part>Continues the same string on the next line, from the left edge

Python's language reference lists \<newline> as "Ignored end of line": "A backslash can be added at the end of a line to ignore the newline".

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​

continue_line.py
message = "This sentence is long, so the code continues it \
on the next line, but it prints as one line."
print(message)

text = """First line, \
still the first line.
Second line."""
print(text)

The continue_line.py script in a macOS-style code window: a normal string continued with a backslash at the end of the first line, and a triple-quoted string whose first line ends with a backslash.

In the triple-quoted string, the backslash removes only the first line break; the second one stays.

Step 2: Run It​

Terminal
uv run python continue_line.py
Output
This sentence is long, so the code continues it on the next line, but it prints as one line.
First line, still the first line.
Second line.

The output in a terminal: the first sentence printed on one line, then First line, still the first line. on one line, then Second line.

The space before each backslash is kept, which is why it on and line, still keep their space. The next line's text starts at the left edge, because any indent there would be part of the string.

Common Mistakes​

MistakeWhat happensDo this instead
A space after the backslashSyntaxError: unterminated string literal (detected at line 1): the backslash no longer ends the lineMake the backslash the very last character
Indenting the continued lineThe indent becomes spaces inside the textStart the continued line at the left edge

Troubleshooting​

SymptomLikely causeFix
Two words are glued togetherNo space before the backslashLeave a space before it

FAQ​

Is this the same as continuing a line of code with a backslash?​

Similar, but outside a string a backslash joins two lines of code. Here it removes a line break from the text.

Is there another way to write a long string on several lines?​

Yes: put several strings next to each other inside brackets, and Python joins them into one.

Conclusion​

A backslash at the end of a line inside the string removed that line break, so a sentence written over two lines of code printed as one line, in a normal string and in a triple-quoted one.

Additional Resources​