Skip to content

The text about escaping newlines in triple-qouted strings is misleading #160886

Open

Description

@prodehghan

Documentation

File: https://docs.python.org/3.16/reference/lexical_analysis.html#string-escape-ignore
Section 2.5.4.1 says:

A backslash can be added at the end of a line to ignore the newline:
(code sample)
The same result can be achieved using triple-quoted strings, or parentheses and string literal concatenation.

The current wording suggests that triple-quoted strings alone can split text across source lines without preserving newlines. In fact, unescaped newlines are retained, and omitting them requires the same backslash-newline escape used in ordinary string literals.

I suggest improving the text and clarifying that escaping newlines works the same in single- or triple-qouted string.

I'm creating the PR.

Linked PRs

Activity

  1. prodehghan commented on Oct 29, 2026

    @prodehghan
    ContributorAuthor

    Here is the PR: #160887

  2. terryjreedy commented on Oct 29, 2026

    @terryjreedy
    Member

    The earlier subsection 2.5.1. Triple-quoted strings already says

    Unescaped newlines are also allowed and retained:
    '''This triple-quoted string
    continues on the next line.'''
    

    The same result of ignoring newlines can be done with triple-quoted strings (with backslashes). However, "triple-quoted strings" in your quote above is linked to 2.5. String and Bytes literals rather than specifically to 2.5.1. Triple-quoted strings, where one finds the information you propose to duplicate, along with an example, and I think this link should be fixed. With this fix, I think the text is OK as is.

    'string literal concatenation' is also linked, to 6.2.3.2. String literal concatenation, which has an example of parentheses and concatenation.

  3. prodehghan commented on Oct 29, 2026

    @prodehghan
    ContributorAuthor

    Thanks for reviewing this, Terry. I agree that the link should point to the triple-quoted strings subsection, and that my proposed explanation repeats information already covered there.

    My remaining concern is that placing triple-quoted strings alongside string literal concatenation makes both sound like alternatives to using a backslash. A reader could therefore understand the sentence to mean that triple quotes alone remove the newline.

    Would this shorter wording address that concern without repeating the earlier explanation?

    “The same escape sequence can be used in triple-quoted strings. Alternatively, the same result can be achieved using parentheses and string literal concatenation.”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions