Write a Long String on Multiple Lines in Python

Modified: | Tags: Python, String

PEP8 code checkers like flake8 raise an E501 line too long error when a single line exceeds 80 characters.

This article explains how to break a long string into multiple lines in Python, without inserting any newline characters.

For information on handling strings that include line breaks, refer to the article below:

If you need to wrap or truncate long strings, the textwrap module may be helpful:

If your lines get too long due to method chaining, see the following article:

Line Continuation Character in Python: Backslash (\)

In Python, a backslash (\) is a line continuation character. If a backslash is placed at the end of a line, the line is considered to continue on the next line.

n = 1 + 2 \
    + 3

print(n)
# 6

Also, when multiple string literals are written next to each other, they are automatically concatenated.

s = 'aaa' 'bbb'

print(s)
# aaabbb

These features allow you to split a long string across multiple lines like this:

s = 'https://ja.wikipedia.org/wiki/'\
    '%E3%83%97%E3%83%AD%E3%82%B0%E3%83'\
    '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E'

print(s)
# https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E

However, keep in mind that only string literals (enclosed in quotes) can be joined this way. Trying to concatenate variables without using an operator will result in a SyntaxError:

s_var = 'xxx'

# s = 'aaa' s_var 'bbb'
# SyntaxError: invalid syntax

Use the + operator to concatenate variables and string literals.

s = 'aaa' + s_var + 'bbb'

print(s)
# aaaxxxbbb

Even if you use backslashes for line continuation, the + operator is still required when concatenating variables:

s = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\
    + s_var\
    + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'

print(s)
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

For more on string concatenation techniques, see the following article:

Use Parentheses for Line Continuation

Python allows implicit line continuation within parentheses (), brackets [], or braces {} — meaning you can break a line without needing a backslash.

Since [] and {} are used for lists and sets, respectively, parentheses () are typically used for splitting long strings in this context.

Note that a tuple is defined by commas, not just by enclosing values in parentheses.

Here’s how you can use parentheses to split a long string:

s = ('https://ja.wikipedia.org/wiki/'
     '%E3%83%97%E3%83%AD%E3%82%B0%E3%83'
     '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E')

print(s)
# https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E

If you need to include variables, the + operator is still required:

s_var = 'xxx'

s = ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
     + s_var
     + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')

print(s)
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

Related Categories

Related Articles