I'm sure there's a good reason!

Remco Gerlich scarblac at pino.selwerd.nl
Thu Mar 22 00:16:02 CET 2001


Tim Rowe <digitig at cix.co.uk> wrote in comp.lang.python:
> Can any of the assembled experts explain to me why:
> 	print r"\hello"
> is fine, but
> 	print r"hello\"
> isn't? In other words, why does the backslash escape quotes in a raw 
> string? At one level I'm going to guess (without reading the spec) that 
> it's because "that's what the spec says", but why? I thought the idea of 
> raw strings was that they /didn't/ handle escapes!

They do handle escapes, but leave the backslash inside the string. Without
that it wouldn't be possible to have " inside a raw "" string. Now, you
can't end a raw string with an odd numbers of backslashes... Just some weird
consequence...

r"hello" "\\"

works.

-- 
Remco Gerlich



More information about the Python-list mailing list