Programming Search⌘K

Featureintro

Line structure

Also called logical line, physical line, line continuation, backslash continuation, implicit line joining.

A Python statement ends at the end of a logical line. A logical line is one or more physical lines, joined by a final backslash or by open brackets.

Language
Python

Synopsis

total = 1 + 2 + \
    3 + 4                      # explicit joining: the line ends in a backslash

months = ["Jan", "Feb",        # implicit joining: inside brackets
          "Mar", "Apr"]

print(total, len(months))
Output
10 4

Semantics

r1. A program is a sequence of logical lines. The token NEWLINE ends a logical line, and a statement cannot cross the end of a logical line, except where the syntax allows a NEWLINE, as between the statements of a compound statement.1

r2. A physical line ends with LF, CR LF or CR. Each of the three becomes one LF, "even inside string literals". One file can mix them, and the end of the input ends the last line.1 Example: line-endings.

r3. When a physical line ends in a backslash that is not in a string literal or a comment, the next line joins it. The backslash and the end of line are deleted. A line that ends in a backslash cannot have a comment, and a backslash anywhere else outside a string literal is an error.2 Example: joining, comment-after-backslash.

r4. An expression in parentheses, square brackets or braces can continue over several physical lines with no backslash. These lines can have comments, their indentation does not matter, and they can be blank. There is no NEWLINE token between them.3 Example: line-tokens.

r5. A logical line with only whitespace and maybe a comment is ignored: it makes no NEWLINE token. In the standard interactive interpreter, a completely empty line ends a multi-line statement.4

r6. At the end of the input, the lexical analyzer makes an ENDMARKER token.4 The tokenizer marks a newline that does not end a logical line with the token NL, which the parser ignores.5 Example: line-tokens.

Examples

The tokens at the line ends of a small program (rules r4 to r6). The first two physical lines make one logical line, so only the second one ends with NEWLINE. The comment line and the blank line give NL.

import io
import tokenize

source = "total = (1 +\n         2)\n# a comment\n\nprint(total)\n"
for token in tokenize.generate_tokens(io.StringIO(source).readline):
    if token.type in (tokenize.NEWLINE, tokenize.NL, tokenize.ENDMARKER):
        print(token.start[0], tokenize.tok_name[token.type])
Output
1 NL
2 NEWLINE
3 NL
4 NL
5 NEWLINE
6 ENDMARKER

The three line ends become LF, also inside a string literal (rule r2).

source = b"text = '''one\r\ntwo\rthree\nfour'''\nprint(repr(text))\n"
exec(compile(source, "demo.py", "exec"))
Output
'one\ntwo\nthree\nfour'

Pitfalls

A comment or a space after the backslash

The backslash must be the last character of the line (rule r3). A comment after it, or a space that the editor does not show, is a SyntaxError.

This code raises SyntaxError, on purpose.

total = 1 + 2 + \  # add the rest
    3 + 4

PEP 8 prefers the brackets of rule r4 to the backslash: "Long lines can be broken over multiple lines by wrapping expressions in parentheses."6

A missing comma inside brackets

Inside brackets the line ends do not count (rule r4). Two string literals with only a line end between them join into one string, so a missing comma loses an item with no error.3 7

This code runs but gives a wrong answer on purpose.

colours = [
    "red",
    "green"
    "blue",
]
print(len(colours), colours)
Output
2 ['red', 'greenblue']

Comparison

Go ends statements with semicolons, and its lexer inserts a semicolon after the last token of a line when that token can end a statement, such as an identifier, a literal or a closing bracket.8 Python has no such insertion step: the end of a logical line is the NEWLINE token, and open brackets join the lines (rule r4).

References

  1. The Python Language Reference, Python 3.15, 2.1 Line structure; 2.1.1 Logical lines; 2.1.2 Physical lines
  2. The Python Language Reference, Python 3.15, 2.1.5 Explicit line joining
  3. The Python Language Reference, Python 3.15, 2.1.6 Implicit line joining
  4. The Python Language Reference, Python 3.15, 2.1.7 Blank lines; 2.1.10 End marker
  5. token, Python 3.15 documentation, token.NEWLINE, token.NL, token.ENDMARKER
  6. PEP 8: Style Guide for Python Code, Maximum Line Length
  7. The Python Language Reference, Python 3.15, 6.2.3.2 String literal concatenation
  8. The Go Programming Language Specification, Semicolons

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source