r"\"" ??? (was A simple single line, triple-quoted comment)

Rustom Mody rustompmody at gmail.com
Fri Apr 3 17:14:17 CEST 2015


On Friday, April 3, 2015 at 8:10:54 PM UTC+5:30, Chris Angelico wrote:
> On Fri, Apr 3, 2015 at 11:52 PM, Rustom Mody  wrote:
> > Speaking about silliness of definitions, I was knocked out in class by this today:
> >
> >>>> r"\""
> > '\\"'
> >
> > Seeing the docs
> > https://docs.python.org/3.4/reference/lexical_analysis.html#string-and-bytes-literals
> > it talks of this explicitly
> >
> > But I still find it strange -- I would have expected this kind of error behavior:
> >
> >>>> "A string" "another incomplete
> > SyntaxError: EOL while scanning string literal
> >>>>
> 
> I'm sorry, I'm not understanding your confusion here. A raw string
> literal allows escaped quote characters - is that the problem?
> 
> ChrisA

Yeah...
I guess you could call it that. Raw? half-cooked? Or double-cooked?

# This looks properly raw
>>> r"\a"
'\\a'
>>> 
# This not so surprising
>>> r"\\"
'\\\\'
#  Thats 2 not 4 backslashes  - still ok
>>> len(r"\\")
2
# Until you try this
>>> r"\"
SyntaxError: EOL while scanning string literal
#... which seems to be 'correctable' by
>>> r"\""
'\\"'
>>> 

In short I see some behavior
What's the pattern or intention I dont get.

Does a backslash escape?? Seems to depend on the moonphase¹ <wink>

>>> print ("\n")


>>> print (r"\n")
\n
-------
¹ Worsened by eclipse tomorrow



More information about the Python-list mailing list