3

If I type this in the interactive interpreter of Python:

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

Why is the escape character (\) printed in the second case? I realize that I can use the print() function to escape it, i.e. to not print it.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
jalalhugo
  • 65
  • 5
  • 2
    Its the Inner double quotes, this seems to be forcing the python interpreter to continue using single quotes and escapes the single quote to avoid confusion. I'm sure theres a better explanation though – Sayse Feb 08 '20 at 20:28

2 Answers2

3

The single quote needs to be escaped because the string is delimited with single quotes. If it weren't escaped then it would signal the end of the literal.

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

If the string were reproduced with double quotes on the outside then the double quotes would need to be escaped:

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

Both forms represent the exact same string. As it happens, repr() chooses to use single quotes and shows it the first way.

user2357112
  • 260,549
  • 28
  • 431
  • 505
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • The question is not why do I need to escape the inner single quote, but why does the interpreter print the escape character in the second case. – jalalhugo Feb 08 '20 at 22:22
  • 1
    It prints valid Python code, something you could type yourself. You have to escape the single quote, so it does too. – John Kugelman Feb 09 '20 at 00:26
1

I would like to build on John's helpful answer.

After examining the docs, I've determined that the string will output DOUBLE quotes if it passes DOUBLE the tests:

  1. Contains a single quote
  2. Contains NO double quotes

That's the easiest manner to remember it.

The literal "doesn't" passes the first test because of the apostrophe, which counts as a single quote. Then, we re-examine it and find it does not contain any double quotes inside of the enclosure. Therefore, the string literal outputs as a double quote:

>>> "doesn't"
"doesn't"

There is no need to escape a single quote with a backslash in the output because the enclosure is composed of double quotes!

Now consider the literal '"Isn\'t," they said.' This literal passes the first test because it contains an apostrophe, even if it's escaped. However, it also contains double quotes, so it fails the second test. Therefore, it outputs as a single quote:

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

And because the enclosure is composed of single quotes, the escape is needed in the output.

If it weren't for the backslash in the output, the EOL (end of line) would have been reached while scanning the literal.

Finally, consider "\"Isn't,\" they said."

There is a single quote inside of the literal, so it does pass the first test...but fails the second. The output should enclose the string literal in single quotes:

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

Therefore, the escape is needed to prevent a premature EOL.