Escape special characters in a Python string
Question:
Does Python have a function that I can use to escape special characters in a string?
For example, I'm "stuck" :
should become I'm "stuck" :\
.
Answers:
Use re.escape
>>> import re
>>> re.escape(r' a.*$')
'\\\ a\.\*\$'
>>> print(re.escape(r' a.*$'))
\ a.*$
>>> re.escape('www.stackoverflow.com')
'www\.stackoverflow\.com'
>>> print(re.escape('www.stackoverflow.com'))
www.stackoverflow.com
Repeating it here:
re.escape(string)
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
As of Python 3.7 re.escape()
was changed to escape only characters which are meaningful to regex operations.
Note: This answer was written in response to the original question which was written in a way that it asked for a generic “function which can [be used] to escape special characters”, without specifying that these would be used for regular expressions, and without further specifying what special characters would have to be escaped.
In order to escape an arbitrary set of “special characters”, you can write a custom function that replaces each of these characters with an escaped variant. Something like this:
def escapeSpecialCharacters ( text, characters ):
for character in characters:
text = text.replace( character, '\' + character )
return text
>>> escapeSpecialCharacters( 'I'm "stuck" :\', ''"' )
'I\'m \"stuck\" :\'
>>> print( _ )
I'm "stuck" :
Use repr()[1:-1]. In this case, the double quotes don’t need to be escaped. The [-1:1] slice is to remove the single quote from the beginning and the end.
>>> x = raw_input()
I'm "stuck" :
>>> print x
I'm "stuck" :
>>> print repr(x)[1:-1]
I'm "stuck" :\
Or maybe you just want to escape a phrase to paste into your program? If so, do this:
>>> raw_input()
I'm "stuck" :
'I'm "stuck" :\'
I’m surprised no one has mentioned using regular expressions via re.sub()
:
import re
print re.sub(r'(["])', r'\1', 'it's "this"') # it's "this"
print re.sub(r"(['])", r'\1', 'it's "this"') # it's "this"
print re.sub(r'([" '])', r'\1', 'it's "this"') # it's "this"
Important things to note:
- In the search pattern, include
as well as the character(s) you’re looking for.
You’re going to be using
to escape your characters, so you need to escape
that as well.
- Put parentheses around the search pattern, e.g.
(["])
, so that the substitution
pattern can use the found character when it adds
in front of it. (That’s what
1
does: uses the value of the first parenthesized group.)
- The
r
in front of r'(["])'
means it’s a raw string. Raw strings use different
rules for escaping backslashes. To write (["])
as a plain string, you’d need to
double all the backslashes and write '([\"])'
. Raw strings are friendlier when
you’re writing regular expressions.
- In the substitution pattern, you need to escape
to distinguish it from a
backslash that precedes a substitution group, e.g. 1
, hence r'\1'
. To write
that as a plain string, you’d need '\\\1'
— and nobody wants that.
As it was mentioned above, the answer depends on your case. If you want to escape a string for a regular expression then you should use re.escape(). But if you want to escape a specific set of characters then use this lambda function:
>>> escape = lambda s, escapechar, specialchars: "".join(escapechar + c if c in specialchars or c == escapechar else c for c in s)
>>> s = raw_input()
I'm "stuck" :
>>> print s
I'm "stuck" :
>>> print escape(s, "\", ['"'])
I'm "stuck" :\
If you only want to replace some characters you could use this:
import re
print re.sub(r'([.\+*?[^]$(){}!<>|:-])', r'\1', "example string.")
use json:
import json
print(r"""(I'm "stuck" :)""") # (I'm "stuck" :)
print(json.dumps(r"""(I'm "stuck" :)""")) # (I'm "stuck" :)
for json to string with escape character
json.dumps(json.dumps(d))
Does Python have a function that I can use to escape special characters in a string?
For example, I'm "stuck" :
should become I'm "stuck" :\
.
Use re.escape
>>> import re
>>> re.escape(r' a.*$')
'\\\ a\.\*\$'
>>> print(re.escape(r' a.*$'))
\ a.*$
>>> re.escape('www.stackoverflow.com')
'www\.stackoverflow\.com'
>>> print(re.escape('www.stackoverflow.com'))
www.stackoverflow.com
Repeating it here:
re.escape(string)
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
As of Python 3.7 re.escape()
was changed to escape only characters which are meaningful to regex operations.
Note: This answer was written in response to the original question which was written in a way that it asked for a generic “function which can [be used] to escape special characters”, without specifying that these would be used for regular expressions, and without further specifying what special characters would have to be escaped.
In order to escape an arbitrary set of “special characters”, you can write a custom function that replaces each of these characters with an escaped variant. Something like this:
def escapeSpecialCharacters ( text, characters ):
for character in characters:
text = text.replace( character, '\' + character )
return text
>>> escapeSpecialCharacters( 'I'm "stuck" :\', ''"' )
'I\'m \"stuck\" :\'
>>> print( _ )
I'm "stuck" :
Use repr()[1:-1]. In this case, the double quotes don’t need to be escaped. The [-1:1] slice is to remove the single quote from the beginning and the end.
>>> x = raw_input()
I'm "stuck" :
>>> print x
I'm "stuck" :
>>> print repr(x)[1:-1]
I'm "stuck" :\
Or maybe you just want to escape a phrase to paste into your program? If so, do this:
>>> raw_input()
I'm "stuck" :
'I'm "stuck" :\'
I’m surprised no one has mentioned using regular expressions via re.sub()
:
import re
print re.sub(r'(["])', r'\1', 'it's "this"') # it's "this"
print re.sub(r"(['])", r'\1', 'it's "this"') # it's "this"
print re.sub(r'([" '])', r'\1', 'it's "this"') # it's "this"
Important things to note:
- In the search pattern, include
as well as the character(s) you’re looking for.
You’re going to be usingto escape your characters, so you need to escape
that as well. - Put parentheses around the search pattern, e.g.
(["])
, so that the substitution
pattern can use the found character when it addsin front of it. (That’s what
1
does: uses the value of the first parenthesized group.) - The
r
in front ofr'(["])'
means it’s a raw string. Raw strings use different
rules for escaping backslashes. To write(["])
as a plain string, you’d need to
double all the backslashes and write'([\"])'
. Raw strings are friendlier when
you’re writing regular expressions. - In the substitution pattern, you need to escape
to distinguish it from a
backslash that precedes a substitution group, e.g.1
, hencer'\1'
. To write
that as a plain string, you’d need'\\\1'
— and nobody wants that.
As it was mentioned above, the answer depends on your case. If you want to escape a string for a regular expression then you should use re.escape(). But if you want to escape a specific set of characters then use this lambda function:
>>> escape = lambda s, escapechar, specialchars: "".join(escapechar + c if c in specialchars or c == escapechar else c for c in s)
>>> s = raw_input()
I'm "stuck" :
>>> print s
I'm "stuck" :
>>> print escape(s, "\", ['"'])
I'm "stuck" :\
If you only want to replace some characters you could use this:
import re
print re.sub(r'([.\+*?[^]$(){}!<>|:-])', r'\1', "example string.")
use json:
import json
print(r"""(I'm "stuck" :)""") # (I'm "stuck" :)
print(json.dumps(r"""(I'm "stuck" :)""")) # (I'm "stuck" :)
for json to string with escape character
json.dumps(json.dumps(d))