1

I'm using python3.7 and typed the following code in python shell:

>>> s0 = r'\c'
>>> s0
'\\c'
>>> print(s0)
\c

>>> s1 = '\c'
>>> s1
'\\c'
>>> print(s1)
\c

>>> s2 = '\\c'
>>> s2
'\\c'
>>> print(s2)
\c

My questions:

  1. Why does python automatically add \ to s1 instead of raising error? Can anyone provide link of this feature (auto \ adding) in python docs?

  2. Do s0, s1 and s2 store the same data internally?

mzoz
  • 1,273
  • 1
  • 14
  • 28
  • Why should `s1` raise an error? – Aran-Fey Aug 21 '18 at 07:23
  • ```s0``` is raw-string, python escape special characters for raw strings – dorintufar Aug 21 '18 at 07:25
  • 1
    @Aran-Fey In many languages, including C, if `\c` is not a defined backslash escape sequence, it means `c`. In a few languages, including Python, it means `\c`. In some languages, to avoid confusion about whether they're like C or like Python, not to mention avoiding the inevitable errors when novices use Windows pathnames and `C:\Hello` works but `C:\hello` doesn't, it's an error. – abarnert Aug 21 '18 at 07:25
  • @dorintufar s0 it's a raw string, not regex – NobbyNobbs Aug 21 '18 at 07:25
  • 1
    @Aran-Fey Actually, in Python, `\c` meaning `\c` is deprecated since 3.6, and it will change to being an error in some future version. (IIRC, it was originally planned for 3.8, but was postponed to some future version to be named later.) See the end of [String and Bytes literals](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals). – abarnert Aug 21 '18 at 07:28
  • @Aran-Fey yes, you are right, they are raw strings, my bad, was confused for a moment – dorintufar Aug 21 '18 at 07:29
  • 1
    Ok, the duplicate doesn't address the "why isn't it an error" part of the question, but then again there are 3 different questions in this question. If we narrow down the question to just "why don't invalid escape sequences throw errors", I'll reopen the question. But otherwise, I'm in favor of leaving it closed. – Aran-Fey Aug 21 '18 at 07:31
  • @abarnert thank you very much for the link! I agree this should raise syntax error to keep consistency. – mzoz Aug 21 '18 at 07:33
  • 1
    @mzoz The Python core devs agree, they just have to decide how long to give people to fix their code… – abarnert Aug 21 '18 at 07:43
  • 1
    @Aran-Fey Since the answer to "why isn't it an error" is really just "it is, just not yet", and the history of why the change was lost in the 3.0 process is probably not very interesting, and the reason why Guido used a different rule from C in the first place is probably lost in the mists of time and would require random wild guessing, I don't think we need a separate question on that. (But I did add a secondary answer to the other question pointing out that it will be an error in the future.) – abarnert Aug 21 '18 at 07:46

1 Answers1

-1

The Python interpreter uses the repr() function to indicate that it's a literal backslash

>>> s2 = '\\c'
>>>len(s2)
2
>>>print s2
\c
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61