4.15 Multiple Statements & Line Continuation
The semicolon, backslash, and implicit-bracket ways to bend Python's line-based syntax — combining statements onto one line or splitting one across several.
Three ways Python’s line-based syntax can be bent — combining lines together, or splitting one statement across several lines.
Semicolon
What Is It?
Separates multiple simple statements placed on one physical line.
Why Is It Rarely Used?
Rarely used by convention (see 4.16 Coding Standards (PEP 8)), but valid syntax.
>>> a = 1; b = 2; print(a + b)
3
Backslash
What Is It?
An explicit line-continuation marker: the backslash at the end of a line tells Python “this statement isn’t finished yet, keep reading the next line.”
total = 1 + \
2 + \
3
>>> total
6
Implicit Continuation
What Is It?
Inside any open bracket — (), [], or {} — Python automatically treats newlines as continuations, no backslash needed.
Why Is It Preferred?
It’s visually cleaner and less error-prone than backslashes (a trailing space after a backslash silently breaks it).
nums = (1 +
2 +
3)
>>> nums
6
Quick Interview Answer
“Python offers three ways to bend its one-statement-per-line rule: semicolons combine statements onto one line (rarely used), a trailing backslash explicitly continues a statement onto the next line, and any open bracket —
(),[],{}— implicitly continues across lines with no backslash needed. Implicit continuation inside brackets is the preferred style since a stray trailing space silently breaks a backslash continuation.”
Common Mistakes
- Leaving a trailing space after a line-continuation backslash — it silently breaks the continuation and raises a
SyntaxErroron the next line. - Reaching for backslash continuation when the expression is already inside brackets, where implicit continuation would work without the backslash at all.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form