representing a literal single slash

Duncan Booth duncan at
Mon Sep 16 12:22:38 CEST 2002

Padraig Brady <Padraig at> wrote in 
news:xZhh9.4239$cP3.10322 at

>> Specifically, a raw string cannot end in a single backslash (since 
> > the backslash would escape the following quote character)
> But there is no escaping when the prefix r is used?
> I guess there is a good reason for this inconsitency?

There is escaping with the prefix r: backslash followed by a quote escapes 
the quote into the string, but leaves the backslash in the string.

Whether this is a good reason depends on your viewpoint. Whether there is 
good reason for raw strings is also arguable: raw strings do not permit you 
to do anything that you could not do with non-raw strings, they violate the 
'one way to do it' rule, and they are confusing.

They do make it easier to write regular expressions, and a regular 
expression couldn't end in a single backslash anyway. If you only ever use 
raw strings for regular expressions then you are unlikely to have 
problems[1].  They are often misused to write DOS pathnames, which may end 
in a backslash.

[1] Unlikely to have problems with the raw strings that is. Problems with 
regular expressions are likely.

Duncan Booth                                             duncan at
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

More information about the Python-list mailing list