doubling slashes in a string
Alex Martelli
aleaxit at yahoo.com
Mon Oct 9 07:10:27 EDT 2000
<piet at cs.uu.nl> wrote in message news:uy9zytl1w.fsf at cs.uu.nl...
[snip]
> >> newname = string.join(string.split(oldname,"\\"), "\\\\")
[snip]
> AM> newname = oldname.split(r'\').join(r'\\')
> AM> newname = oldname.replace(r'\', r'\\')
>
> AM> would strike some of us as being even better yet!-)
>
> It would strike most of us as being particularly bad, especially those who
> would try it. (A string cannot end in an odd number of \, not even a raw
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_...?
Oh well, then I guess my above-quoted suggestions will have to be reworded:
newname = oldname.split('\\').join(r'\\')
newname = oldname.replace('\\', r'\\')
Alex
More information about the Python-list
mailing list