You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.'
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).
@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.
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.
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.
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.
Activity
zware commentedon Nov 29, 2025
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 toprintthe 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:
which becomes
terryjreedy commentedon Nov 29, 2025
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.
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/toTo 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).StanFromIreland commentedon Dec 26, 2025
Closing as no there has been no response from the issue author for a month.
terryjreedy commentedon Dec 26, 2025
@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.
StanFromIreland commentedon Mar 1, 2026
A second user has reported this: #145383. @zvikadror, what you think of Terry's suggestion?
zvikadror commentedon Mar 1, 2026
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:
then add 2 double quotes ANYWHERE in the string:
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.
StanFromIreland commentedon Mar 1, 2026
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:
zware commentedon May 15, 2026
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 aprint('"Isn\'t," they said.')to the end of the example with a comment should help to show that.