Quoting backslashes in Python string literals
PythonStringPython Problem Overview
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?
Python Solutions
Solution 1 - Python
You're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)
>>> foo = 'baz "\\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
Incidentally, there's another string form which might be a bit clearer:
>>> print(r'baz "\"')
baz "\"
Solution 2 - Python
Use a raw string:
>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
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 the print
ed string):
>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
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:
>>> foo = r'baz \'
File "<stdin>", line 1
foo = r'baz \'
^
SyntaxError: EOL while scanning single-quoted string
Raw strings don't work properly when you do that. You have to use a regular string and escape your backslashes:
>>> foo = 'baz \\'
>>> print(foo)
baz \
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:
myfile = os.path.normpath('c:/folder/subfolder/file.txt')
open(myfile)
This will save a lot of escaping and hair-tearing. This page was handy when going through this a while ago.
Solution 3 - Python
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.
infile = open('c:/folder/subfolder/file.txt')
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 the subprocess
module).
Solution 4 - Python
If you are here for a filepath just use "\\"
import os
path = r"c:\file"+"\\"+"path"
os.path.normpath(path)
which will outputc:\file\path
Solution 5 - Python
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:
newString = string1 + "\ ".strip() + string2