← 输入框|github.com|刷新
Skip to content

Quoting a quote - small mistake Isn\'t #142049

Open

Description

@derschaumtastenfinder

Documentation

Within the python tutorial, there is a chapter dealing with quoting a quote. I think, the last example is incorrect here:

'"Isn\'t," they said.' -----> supposed to return '"Isn\'t," they said.'

Shouldn't the return be "Isn't," they said.?

Linked PRs

Activity

  1. added
    docs
    on Nov 28, 2025
  2. zware commented on Nov 29, 2025

    @zware
    Member

    No; that example is trying to show that when a string contains both kinds of quote character, one of them has to be quoted. str.__repr__ uses single quotes unless the string contains at least one single quote (and no double quotes), so when both are present any single quotes get escaped. If you were to print the string or write it to a file, it would not contain any backslashes.

    If you have any suggestions to make that example clearer, this is not the first report that we've had of this :)


    Sidenote: backslash escaping is fairly ubiquitous, and actually caused problems with your post here. In GitHub issues or other places that accept Markdown, you can use ` characters to surround anything that should be rendered as code, like so:

    `'"Isn\'t," they said.'` -----> supposed to return `'"Isn\'t," they said.'`
    

    which becomes

    '"Isn\'t," they said.' -----> supposed to return '"Isn\'t," they said.'

  3. terryjreedy commented on Nov 29, 2025

    @terryjreedy
    Member

    The example is a paragraph within this section. One can avoid the backslash on entry by using triple quotes, but the repr() result will not use them.

    """"Isn'," they said"""
    '"Isn\'," they said'
    

    There are at least 2 problems with explanations here. First, we have not and probably do not want to introduce triple quotes here (which could not be used within a string). Second, I believe we have not and probably do not want to introduce the concept of 'builtin function' and in particular the difference between 'str' and 'repr'.

    But we can better explain the difference between a quote as string maker and a quote as a string content character. I suggest replacing/expanding To quote a quote, we need to “escape” it, by preceding it with \. Alternatively, we can use the other type of quotation marks: by/to To insert a quote character in a string delimited by the same quotes, we must "escape" it by preceding it with \. This tells Python to not interpret the quote as ending the string. Alternatively, if we are not using the other type quote within the string, we can use that other quote to delimit the string. Follow with the first 4 of the 5 examples, switching the order of 3 and 4 to match the order of 1 and 2, which start with the \ alternative. Then add another explanatory sentence. If both quotes are used within a string, pick one for the delimiter pair and escape it with \ within. Follow with the fifth example. @derschaumtastenfinder Is this clear enough?

    I should hope that non-beginners knowing about triple backquotes will understand why I omitted mention of them in a tutorial for beginning beginners. (Besides adding their own confusion, they are the worse solution in most, perhaps nearly all, cases of strings containing both quote chars.)

    Sidenote (continued): in the same places, like here, names with underscores, like __init__, also need backticks to not appear like init (slightly bold init instead of with the underscores).

  4. added
    pending
    on Nov 29, 2025
  5. StanFromIreland commented on Dec 26, 2025

    @StanFromIreland
    Member

    Closing as no there has been no response from the issue author for a month.

  6. terryjreedy commented on Dec 26, 2025

    @terryjreedy
    Member

    @zware and @StanFromIreland If either of you think my suggested change is an improvement, we can make it or something similar without waiting for one person to respond. If not, leave it closed.

  7. StanFromIreland commented on Mar 1, 2026

    @StanFromIreland
    Member

    A second user has reported this: #145383. @zvikadror, what you think of Terry's suggestion?

  8. zvikadror commented on Mar 1, 2026

    @zvikadror

    Thanks Stan
    I'm coming from PHP and started reading the Python manual to do some cross coding in order to add hands on practice, really from scratch. So any prior changes and string handling history is not familiar to me when I visit the actual escaping functionality.
    Within single quotes, when a single quote is escaped, it should appear as single quote without the escape char.
    Double quotes are just characters in context of single quotes encapsulation, so, they appear as chars.
    So I took the documented example to online python interpreter and the escaped single quote appeared as single quote, no escape character, as I would expect reading the reference (you should always guess the output and only then check it, right?).
    So long story short, I cannot really give any extra thought about Terry's deeper knowledge of Python's string handling, sorry.
    The text line I used on the interpreter was : '"Isn't," they said.'
    Going further, I tested on Windows interactive Python and indeed, the output was as the manual says.
    I THINK IT IS WRONG, because if you take this line, working as expected:

    >>> 'Isn\'t, they said.'
    "Isn't, they said."
    

    then add 2 double quotes ANYWHERE in the string:

    >>> 'Isn\'t, they said.""'
    'Isn\'t, they said.""'
    

    You get the escaped slash inside the text, and it seems to me like a bug in the interpreter, it changes the context when all I wanted is just to add 2 double quotes as characters, not change the escaped single quote.

  9. StanFromIreland commented on Mar 1, 2026

    @StanFromIreland
    Member

    I cannot really give any extra thought about Terry's deeper knowledge of Python's string handling

    Oh sorry that's not what I meant, Terry proposed an update to make the tutorial simpler/understandable, so I'm curious whether his proposal would have resolved your confusion:

    But we can better explain the difference between a quote as string maker and a quote as a string content character. I suggest replacing/expanding To quote a quote, we need to “escape” it, by preceding it with . Alternatively, we can use the other type of quotation marks: by/to To insert a quote character in a string delimited by the same quotes, we must "escape" it by preceding it with . This tells Python to not interpret the quote as ending the string. Alternatively, if we are not using the other type quote within the string, we can use that other quote to delimit the string. Follow with the first 4 of the 5 examples, switching the order of 3 and 4 to match the order of 1 and 2, which start with the \ alternative. Then add another explanatory sentence. If both quotes are used within a string, pick one for the delimiter pair and escape it with \ within. Follow with the fifth example.

  10. zware commented on May 15, 2026

    @zware
    Member

    Stumbling upon this in an old open tab, I think what we really need here might be to clarify that the \ added to the repr is just that: part of the repr, not part of the string itself. Just adding a print('"Isn\'t," they said.') to the end of the example with a comment should help to show that.

  11. removed
    pending
    on Nov 28, 2026
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

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions