Raw String Question

MRAB google at mrabarnett.plus.com
Fri Mar 13 10:28:50 EDT 2009


andrew cooke wrote:
> MRAB wrote:
>> andrew cooke wrote:
>>> MRAB wrote:
>>> [...]
>>>> The other special case is with \u in a Unicode string:
>>>>
>>>>  >>> ur"\u0041"
>>>> u'A'
>>> this isn't true for 3.0:
>>>
>>>>>> r"\u0041"
>>> '\\u0041'
>>>
>>> (there's no "u" because it's a string, not a bytes literal)
>>>
>>> and as far as i can tell, that's correct behaviour according to the
>>> docs.
>>>
>>  From the 3.0 docs "Even in a raw string, string quotes can be escaped
>> with a backslash, but the backslash remains in the string". Seems a bit
>> pointless to me. I would've preferred the backslash to have no special
>> behaviour at all. Simpler, IMHO...
> 
> not sure what you are implying here.  i understood "string quotes" in the
> text you quote (which i had read) to mean \" and \', which is the
> behaviour the original poster saw (and why you cannot end a string with a
> slash).
> 
> however, you seem to think "string quotes" are \u escapes?
 >
Huh? I don't know why you think that.

> did you see:
> 
>   As a result, '\U' and '\u' escapes in raw strings are not
>   treated specially.
> 
> a few paragraphs above?
> 
> also,
> 
>>>> len(r"\u0041")
> 6
> 
My point is this:

In Python 3.x a backslash doesn't have a special meaning in a raw
string, except that it can prevent a following quote from ending the
string, but the backslash is still included. Why? How useful is that? I
think it would've been simpler if a backslash had _no_ special effect,
not even with a following quote. If you want a quote then either use the
other quote character as the delimiter or use a triple-quoted raw
string.



More information about the Python-list mailing list