Simple syntax question

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon May 12 04:24:36 EDT 2003


Erik Max Francis <max at alcyone.com> wrote in 
news:3EBDF606.F48B7E76 at alcyone.com:

> It indicates a "raw" string literal.  In raw string literals, escape
> sequences aren't processed, so a backslash has no special meaning there:
> 
>>>> '\n' # this is a newline
> '\n'
>>>> '\\n' # this is a backslash and an n
> '\\n'
>>>> r'\n' # this is a backslash and an n, too
> '\\n'
> 
> They're useful for when you want to build a string that contains a lot
> of backslashes, such as in regular expression strings.
> 

A minor nitpick. A backslash does still have a special meaning in a raw 
string if it is followed by a quote: then the backslash prevents the 
following quote terminating the string, although the backslash itself is 
not removed from the string.

e.g.
>>> s = r"\""
>>> s
'\\"'

-- 
Duncan Booth                                             duncan at rcp.co.uk
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