When using PEP8 code checkers such as flake8 in Python, an error, E501 line too long
, is raised when one line exceeds 80 characters.
This article describes how to write a long string that does not contain a new line on multiple lines.
- Use a backslash [
\
] as a line continuation character - Use parentheses
See the following article for various operations related to strings with line breaks.
- Handle line breaks [newlines] in Python
If you want to wrap or truncate long strings, the textwrap module is useful. See the following article.
- Wrap and truncate a string with textwrap in Python
If the number of characters in a line becomes too long due to method chaining, you can break the line in the same way.
- Method chains with line breaks in Python
Use a backslash [\
] as a line continuation character
In Python, a backslash [\
] is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.
n = 1 + 2 \
+ 3
print[n]
# 6
Also, if multiple string literals are written sequentially, they are concatenated into one string as follows:
s = 'aaa' 'bbb'
print[s]
# aaabbb
Therefore, you can write a long string into multiple lines as follows:
s = '//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]
# //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
Only string literals [string surrounded by '
or "
] are concatenated if written consecutively. Note that in the case of variables, an error is raised.
s_var = 'xxx'
# s = 'aaa' s_var 'bbb'
# SyntaxError: invalid syntax
Use the +
operator to concatenate variables, or variables and string literals.
s = 'aaa' + s_var + 'bbb'
print[s]
# aaaxxxbbb
You need the +
operator to concatenate variables, even if they are separated by a backslash [\
].
s = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\
+ s_var\
+ 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
print[s]
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
See the following article for details of string concatenation.
- Concatenate strings in Python [+ operator, join, etc.]
Use parentheses
In Python, you can freely break the line in parentheses [[]
, {}
,
[]
]. Using this rule, you can write a long string on multiple lines with parentheses instead of backslashes.
Since {}
is used for set
and []
is used for list
, use []
for such purpose. Note that tuple
is created by commas, not []
.
- A tuple with one element requires a comma in Python
You can write as follows.
s = ['//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]
# //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
variables are included, you need the +
operator.
s = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
+ s_var
+ 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb']
print[s]
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb