The result '\\&' is only displayed - actually the string is \&:
Copy>>> str = '&'
>>> new_str = str.replace('&', '\&')
>>> new_str
'\\&'
>>> print new_str
\&
Try it in a shell.
Answer from Emil Ivanov on Stack OverflowI need to make a string that contains "\" but it doesn't work
SyntaxError: unterminated string literal (detected at line 15)
The result '\\&' is only displayed - actually the string is \&:
Copy>>> str = '&'
>>> new_str = str.replace('&', '\&')
>>> new_str
'\\&'
>>> print new_str
\&
Try it in a shell.
The extra backslash is not actually added; it's just added by the repr() function to indicate that it's a literal backslash. The Python interpreter uses the repr() function (which calls __repr__() on the object) when the result of an expression needs to be printed:
Copy>>> '\\'
'\\'
>>> print '\\'
\
>>> print '\\'.__repr__()
'\\'
python - How can I put an actual backslash in a string literal (not use it for an escape sequence)? - Stack Overflow
Python escaping backslash - Stack Overflow
can't create a string that contains "\"
problem with ignoring the escape character in a python string
Videos
To answer your question directly, put r in front of the string.
final= path + r'\xulrunner.exe ' + path + r'\application.ini'
More on Python's site here
Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters
But a better solution would be os.path.join:
final = (os.path.join(path, 'xulrunner.exe') + ' ' +
os.path.join(path, 'application.ini'))
(I split this across two lines for readability, but you could put the whole thing on one line if you want.)
I will mention that you can use forward slashes in file paths, and Python will automatically convert them to the correct separator (backslash on Windows) as necessary. So
final = path + '/xulrunner.exe ' + path + '/application.ini'
should work. But it's still preferable to use os.path.join because that makes it clear what you're trying to do.
You can escape the slash. Use \\ and you get just one slash.
