How to write very long string that conforms with PEP8 and prevent E501
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
PEP 8 recommends a maximum line length of 79 characters (or 99/120 for many projects). The E501 lint error triggers when a line exceeds this limit. Long strings — URLs, SQL queries, log messages — frequently exceed this threshold. Python provides several ways to split long strings across multiple lines without changing their content: implicit string concatenation, parenthesized strings, backslash continuation, textwrap.dedent, and f-string splitting. The right approach depends on whether the string contains newlines and how readable the result needs to be.
Implicit String Concatenation (Recommended)
Adjacent string literals are automatically concatenated by the Python compiler. No + operator is needed.
Parenthesized Strings with f-strings
Backslash Continuation
PEP 8 prefers parentheses over backslash continuation.
Long URLs and Paths
Multi-Line SQL Queries
Long Function Arguments
Suppressing E501 for Specific Lines
Common Pitfalls
- Accidentally inserting spaces or newlines: Implicit concatenation joins strings exactly as written.
"hello " "world"produces"hello world"(with a space), but"hello" "world"produces"helloworld"(no space). Always check where spaces go when splitting. - Using
+instead of implicit concatenation:"a" + "b"creates a runtime concatenation, while"a" "b"is compiled into a single string"ab". The+version has (tiny) runtime overhead and is less idiomatic for splitting long literals. - Triple-quoted strings including unwanted indentation:
"""strings preserve all whitespace including indentation in your source code. Usetextwrap.dedent()to strip common leading whitespace, or use parenthesized implicit concatenation for strings that should not contain newlines. - Breaking URLs or file paths: Splitting a URL or file path across lines changes the string content (adds no separator by default). If you forget a space or add an unwanted one, the URL becomes invalid. Keep URLs on one line with
# noqa: E501or build them from parts. - Space after backslash continuation: A backslash followed by a space and then a newline (
\ \n) is a syntax error, not a line continuation. This is invisible and hard to debug. Parenthesized strings do not have this problem, which is why PEP 8 prefers them.
Summary
- Use parenthesized implicit concatenation for long strings:
("part one " "part two") - Use triple-quoted strings with
textwrap.dedent()for multi-line content like SQL - PEP 8 prefers parentheses over backslash continuation
- Use
# noqa: E501for URLs and other strings that should not be split - Configure your linter's
max-line-lengthto match your project's convention (79, 99, or 120)
Related reading
- How vectorizer fit_transform work in sklearn?
- How would I get everything before a in a string Python
- How would you make a comma-separated string from a list of strings?
- HTTP requests and JSON parsing in Python
- Huggingface transformers trainer output not giving any predictions?
- Hyperparameter optimization of MLPRegressor in scikit-learn
- I am not able to import resnet from keras.applications module
- I cannot install aws cli on mac os with pip - awscli command not found
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.