Raw strings and escaping
Jon Ribbens
jon+usenet at unequivocal.co.uk
Tue Oct 3 08:40:12 EDT 2006
In article <Xns9851834924701duncanbooth at 127.0.0.1>, Duncan Booth wrote:
>> I presume there was originally some reason for this bizarre behaviour
>> - it'd be interesting to know what it is/was, if anyone knows?
>>
> See the FAQ for the explanation:
>
> http://www.python.org/doc/faq/general/#why-can-t-raw-strings-r-strings-end-with-a-backslash
>
> The idea is that if you use raw strings for regular expressions you can
> write things like:
>
> pattern = r'[\'"]'
>
> if the raw string simply ignored the backslash altogether it would be much
> harder to write regular expressions that contain both sorts of quotes.
Well, hardly *much* harder:
pattern = r"""foo"""
Personally, I think that raw strings behaving as they do is
unexpected, unintuitive and unnecessary, but it's obviously too late
to change it now anyway ;-)
I think standard strings accepting backslash followed by an unexpected
character is also a mistake, but again it's too late to fix now.
More information about the Python-list
mailing list