I need to escape a &
(ampersand) character in a string. The problem is whenever I string = string.replace ('&', '\&')
the result is '\\&'
. An extra backslash is added to escape the original backslash. How do I remove this extra backslash?
Answer. When a string is double quoted, it is processed by the compiler and again at run-time. Since a backslash (\) is removed whenever the string is processed, the double-quoted string needs double backslashes so that there is one left in the string at run time to escape a "special character".
Control character But escape characters used in programming (such as the backslash, "\") are graphic, hence are not control characters. Conversely most (but not all) of the ASCII "control characters" have some control function in isolation, therefore they are not escape characters.
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.
In short, to match a literal backslash, one has to write '\\\\' as the RE string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal.
The result '\\&'
is only displayed - actually the string is \&
:
>>> str = '&' >>> new_str = str.replace('&', '\&') >>> new_str '\\&' >>> print new_str \&
Try it in a shell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With