How do I escape backslash and single quote or double quote in Python? [duplicate]

How you did it

If your “long string” is read from a file (as you mention in a comment) then your question is misleading. Since you obviously don’t fully understand how escaping works, the question as you wrote it down probably is different from the question you really have.

If these are the contents of your file (51 bytes as shown + maybe one or two end-of-line characters):

some 'long' string \' and \" some 'escaped' strings

then this is what it will look like in python:

>>> s1 = open('data.txt', 'r').read().strip()
>>> s1
'some \'long\' string \\\' and \\" some \'escaped\' strings'
>>> print s1
some 'long' string \' and \" some 'escaped' strings

What you wrote in the question will produce:

>>> s2 = '''some 'long' string \' and \" some 'escaped' strings'''
>>> s2
'some \'long\' string \' and " some \'escaped\' strings'
>>> print s2
some 'long' string ' and " some 'escaped' strings
>>> len(s)
49

Do you see the difference?

There are no backslashes in s2 because they have special meaning when you use them to write down strings in Python. They have no special meaning when you read them from a file.

If you want to write down a string that afterwards has a backslash in it, you have to protect the backslash you enter. You have to keep Python from thinking it has special meaning. You do that by escaping it – with a backslash.

One way to do this is to use backslashes, but often the easier and less confusing way is to use raw strings:

>>> s3 = r'''some 'long' string \' and \" some 'escaped' strings'''
'some \'long\' string \\\' and \\" some \'escaped\' strings'
>>> print s3
some 'long' string \' and \" some 'escaped' strings
>>> s1 == s3
True

How you meant it

The above was only to show you that your question was confusing.

The actual answer is a bit harder – when you are working with regular expressions, the backslash takes on yet another layer of special meaning. If you want to safely get a backslash through string escaping and through regex escaping to the actual regex, you have to write down multiple backslashes accordingly.

Furthermore, rules for putting single quotes (') in single-quoted raw strings (r'') are a bit tricky as well, so I will use a raw string with triple single-quotes (r'''''').

>>> print re.sub(r'''\\['"]''', 'thevalue', s1)
some 'long' string thevalue and thevalue some 'escaped' strings

The two backslashes stay two backslashes throughout string escaping and then become only one backslash without special meaning through regex escaping. In total, the regex says:
“match one backslash followed by either a single-quote or a double-quote.”

How it should be done

Now for the pièce de résistance: The previous is really a good demonstration of what jwz meant1. If you forget about regex (and know about raw strings), the solution becomes much more obvious:

>>> print s1.replace(r'\"', 'thevalue').replace(r"\'", 'thevalue')
some 'long' string thevalue and thevalue some 'escaped' strings

1 Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

Leave a Comment