I’m trying to do a regex to substitute in a backslash, but Python seems to be inserting a double-backslash, and I can’t make it stop!
>>> re.sub('a', '\\ b', 'a')
'\\ b'
Double backslash is supposed to be backslash (escape + backslash = backslash), but it ends up being literal.
If I remove the double slash, it doesn’t print one at all:
>>> re.sub('a', '\ b', 'b')
'b'
How do I get Python to sub in just one backslash?
It’s not inserting a double backslash. That is simply the interactive interpreter showing the string as a string literal. Use
printto see the actual string: