How to Continue a String on the Next Line in Python with a Backslash
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
<name> = "<first part> \
<second part>"
| Part | What it means |
|---|---|
\ at the end of a line | Inside 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
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)

In the triple-quoted string, the backslash removes only the first line break; the second one stays.
Step 2: Run It
uv run python continue_line.py
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 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
| Mistake | What happens | Do this instead |
|---|---|---|
| A space after the backslash | SyntaxError: unterminated string literal (detected at line 1): the backslash no longer ends the line | Make the backslash the very last character |
| Indenting the continued line | The indent becomes spaces inside the text | Start the continued line at the left edge |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Two words are glued together | No space before the backslash | Leave 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
- Ignored end of line — the backslash-newline escape.
