ending a string with a backslash

Dan Bishop danb_83 at yahoo.com
Mon May 1 10:13:32 CEST 2006

John Salerno wrote:
> I have this:
> subdomain = raw_input('Enter subdomain name: ')
> path = r'C:\Documents and Settings\John Salerno\My Documents\My
> Webs\1and1\johnjsalerno.com\' + subdomain
> Obviously the single backslash at the end of 'path' will cause a
> problem, and escaping it with a backslash seems to fix this problem, but
> how does escaping work when I already have it as a raw string? When I
> test it out and then print string, I get something like this:
> C:\Documents and Settings\John Salerno\My Documents\My
> Webs\1and1\johnjsalerno.com\\test
> But I don't see how this is valid, since all the backslashes are single
> (which is correct) except the last one. Somehow this still works when I
> tried to create the new directory -- os.mkdir(path) -- but I wasn't sure
> if this is the right way to go about it, or if there is some other,
> better way to handle the final backslash.

As others have stated, you can use a forward slash.  Alternatively, you
can write:

>>> r'This\string\contains\backslashes' '\\'

More information about the Python-list mailing list