Raw String Question

Duncan Booth duncan.booth at invalid.invalid
Thu Mar 12 20:55:11 CET 2009

Jim Garrison <jgarrison at troux.com> wrote:

> OK, I'm curious as to the reasoning behind saying that
>     When an 'r' or 'R' prefix is present, a character following a
>     backslash is included in the string without change, and all
>     backslashes are left in the string.
> which sounds reasonable, but then saying in effect "Oh wait, let's
> introduce a special case and make it impossible to have a literal
> backslash as the last character of a string without doubling it".
> So you have a construct (r'...') whose sole reason for existence
> is to ignore escapes, but it REQUIRES an escape mechanism for one
> specific case (which comes up frequently in Windows pathnames).

You have a construct whose primary intent is to make it easier to write 
regular expressions without doubling all the backslashes. Regular 
expressions are quite likely to contain both single and double quote marks, 
so you need some way to include both kinds of quote marks. Regular 
expressions also cannot end with a single backslash.

If you need Windows pathnames then you can almost always just write them 
using forward slashes (and use os.path.normpath() on them if you really 
need backslashes).

There are many other strings which cannot be easily represented as a raw 
string: they are a convenience but not a replacement for ordinary string 

More information about the Python-list mailing list