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?
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: