When does the escape character work within raw strings?

Rhodri James rhodri at wildebst.demon.co.uk
Thu May 21 23:44:15 EDT 2009


On Fri, 22 May 2009 03:42:18 +0100, walterbyrd <walterbyrd at iname.com>  
wrote:

> I guess I am confused about when when escape characters are are
> interpersonal as escape characters, and escape characters are not
> treated as escape characters.

No, you're confused about the number of entirely different things
that are interpreting a string, and the difference between a string
literal and a string object.

> Sometimes escape characters in regular strings are treated as escape
> characters, sometimes not. Same seems to go for raw strings. So how do
> I know?
>
> IMO: '\' characters in raw strings should not be given any special
> meaning. That would also solve the common problem of r'c:\whatever\'
> not working in python. But I digress.

Escaping the delimiting quote is the *one* time backslashes have a
special meaning in raw string literals.

> To me this does not seem right. r'\n' should not equal '\n'

And it doesn't.  Let me explain.  No, that would take too long,
let me summarise. :-)

>>>> s = 'x\nx'

`s` is a string object containing the character 'x', a newline, and 'x'.

>>>> a = re.sub('\n', 'x', s)

This calls re.sub with a pattern string object that contains a single
newline character.  Since this character has no special meaning to the
sub function it faithfully searches `s` for newlines, and, finding one,
replaces it with an 'x' character.

>>>> a = re.sub(r'\n', 'x', s)

This calls re.sub with a pattern string object that contains two
characters, a backslash followed by an 'n'.  This combination *does*
have a special meaning to the sub function, which does it's own
translation of the pattern into a single newline character.  Then,
as before, it spots the newline in `s` and replaces it with an 'x'.

Note, however, that the string object created by the raw string literal
was *two* characters long.  It's re.sub (in common with the rest of the
re module functions) that chooses to interpret the backslash specially.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list