Raw String Question

MRAB google at mrabarnett.plus.com
Thu Mar 12 19:31:46 EDT 2009


Jim Garrison wrote:
> Tim Chase wrote:
>>>   >>> r"a\"
>>>   SyntaxError: EOL while scanning string literal (<pyshell#45>, line 1)
>>>
>>> It seems the parser is interpreting the backslash as an escape
>>> character in a raw string if the backslash is the last character.
>>> Is this expected?
>>
>> Yep...as documented[1], "even a raw string cannot end in an odd number 
>> of backslashes".
> 
> So how do you explain this?
> 
>     >>> r'a\'b'
>     "a\\'b"
> 
> The backslash is kept, but it causes the following quote to be escaped.
> 
(The following examples are from Python 2.x.)

The other special case is with \u in a Unicode string:

 >>> ur"\u0041"
u'A'

However, \x isn't special:

 >>> ur"\x41"
u'\\x41'

and \u isn't a recognised escape sequence in a bytestring:

 >>> r"\u0041"
'\\u0041'




More information about the Python-list mailing list