I have a string that contains both double-quotes and backslashes that I want to set to a variable in Python. However, whenever I try to set it, the quotes or slashes are either removed or escaped. Here's an example:
>>> foo = 'baz "\"'
>>> foo
'baz ""'
So instead of baz "\"
like I want I'm getting baz ""
. If I then try to escape the backslash, it doesn't help either:
>>> foo = 'baz "\\"'
>>> foo
'baz "\\"'
Which now matches what I put in but wasn't what I originally wanted. How do you get around this problem?
Use a raw string:
Note that although it looks wrong, it's actually right. There is only one backslash in the string
foo
.This happens because when you just type
foo
at the prompt, python displays the result of__repr__()
on the string. This leads to the following (notice only one backslash and no quotes around theprint
ed string):And let's keep going because there's more backslash tricks. If you want to have a backslash at the end of the string and use the method above you'll come across a problem:
Raw strings don't work properly when you do that. You have to use a regular string and escape your backslashes:
However, if you're working with Windows file names, you're in for some pain. What you want to do is use forward slashes and the
os.path.normpath()
function:This will save a lot of escaping and hair-tearing. This page was handy when going through this a while ago.
Another way to end a string with a backslash is to end the string with a backslash followed by a space, and then call the
.strip()
function on the string.I was trying to concatenate two string variables and have them separated by a backslash, so i used the following:
You're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)
Incidentally, there's another string form which might be a bit clearer:
What Harley said, except the last point - it's not actually necessary to change the '/'s into '\'s before calling open. Windows is quite happy to accept paths with forward slashes.
The only time you're likely to need the string normpathed is if you're passing to to another program via the shell (using
os.system
or thesubprocess
module).