Wow, this should be so simple, but it’ just not working.
I need to inset a “\” into a string (for a Bash command), but escaping just doesn’t work.
>>> a = 'testing'
>>> b = a[:3] + '\' + a[3:]
>>> File "<stdin>", line 1
>>> b = a[:3] + '\' + a[3:]
^
>>>SyntaxError: EOL while scanning string literal
>>> b = a[:3] + '\\' + a[3:]
>>> b
'tes\\ting'
>>> sys.version
'2.7 (r27:82500, Sep 16 2010, 18:02:00) \n[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)]'
The first error is understandable and expexted. The end quote is being eaten, and the interpreter barfs.
However, the second example should work. Why is there two slashes?
Python 2.7
Thanks,
Edit: Thanks Greg. It was a problem with working at the interpreter and not using repr(b). Python was working correctly, but I wasn’t looking at the correct version of the output.
You are being misled by Python’s output. Try: