doubling slashes in a string

Remco Gerlich scarblac at pino.selwerd.nl
Mon Oct 9 09:33:45 EDT 2000


Alex Martelli <aleaxit at yahoo.com> wrote in comp.lang.python:
> Whoops!  You're right.  I had never noticed this lexical peculiarity,
> and now I wonder about the rationale for it -- since backslashes play
> no special role within a rawstring, why the peculiarly specific prohibition
> about having an odd number of them _at the end_...?

They still have a special role - they escape quotes. r"\"" still needs
to be equal to '"'. As a consequence, a single \ at the end of a raw
string always escapes the quote, so the string isn't closed.

> Oh well, then I guess my above-quoted suggestions will have to be reworded:
> 
>     newname = oldname.split('\\').join(r'\\')
>     newname = oldname.replace('\\', r'\\')

Note that if he's doing things with pathnames on Windows, it might
be a lot easier to use / instead of \ to start with (that works).

Remco Gerlich



More information about the Python-list mailing list