The result '\\&'
is only displayed – actually the string is \&
:
>>> str="&"
>>> new_str = str.replace('&', '\&')
>>> new_str
'\\&'
>>> print new_str
\&
Try it in a shell.
More Related Contents:
- How to understand regular expression with python?
- Why do backslashes appear twice?
- Remove specific characters from a string in Python
- How to search and replace text in a file?
- Process escape sequences in a string in Python
- Speed up millions of regex replacements in Python 3
- How can I remove the ANSI escape sequences from a string in python
- Best way to replace multiple characters in a string?
- How to use string.replace() in python 3.x
- How to un-escape a backslash-escaped string?
- String replace doesn’t appear to be working
- How to input a regex in string.replace?
- python replace single backslash with double backslash
- Escape special characters in a Python string
- replacing text in a file with Python
- Removing a list of characters in string
- Replace nth occurrence of substring in string
- How to write string literals in python without having to escape them?
- Keeping only certain characters in a string using Python?
- What does a leading `\x` mean in a Python string `\xaa`
- Python Replace \\ with \ [duplicate]
- Replace first occurrence only of a string?
- Mass string replace in python?
- Replace special characters in a string in Python
- Replacing specific words in a string (Python)
- pandas: replace string with another string
- How can I put an actual backslash in a string literal (not use it for an escape sequence)?
- How to remove all characters before a specific character in Python?
- Mutable and Immutable Strings in python
- Python replace function [replace once]