All given answers will break on general Unicode strings. The following works for Python3 in all cases, as far as I can tell:
from codecs import encode, decode
sample = u'mon€y\\nröcks'
result = decode[encode[sample, 'latin-1', 'backslashreplace'], 'unicode-escape']
print[result]
In recent Python versions, this also works without the import:
sample = u'mon€y\\nröcks'
result = sample.encode['latin-1', 'backslashreplace'].decode['unicode-escape']
As outlined in the comments, you can also use the literal_eval
method from the ast
module like so:
import ast
sample = u'mon€y\\nröcks'
print[ast.literal_eval[F'"{sample}"']]
Or like this when your string really contains a string literal [including the quotes]:
import ast
sample = u'"mon€y\\nröcks"'
print[ast.literal_eval[sample]]
However, if you are uncertain whether the input string uses double or single quotes as delimiters, or when you cannot assume it to be properly escaped at all, then literal_eval
may raise a SyntaxError
while the encode/decode method will still work.
PythonServer Side ProgrammingProgramming
Beyond Basic Programming - Intermediate Python
Most Popular
36 Lectures 3 hours
Mohammad Nauman
More Detail
Practical Machine Learning using Python
Best Seller
91 Lectures 23.5 hours
MANAS DASGUPTA
More Detail
Practical Data Science using Python
22 Lectures 6 hours
MANAS DASGUPTA
More Detail
There are two ways to go about unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:
>>> import ast >>> a = '"Hello,\\nworld"' >>> print ast.literal_eval[a] Hello, world
Another way is to use the decode['string_escape'] method from the string class. For example,
>>> print "Hello,\\nworld".decode['string_escape'] Hello, world
Rajendra Dharmkar
Updated on 30-Sep-2019 07:12:07
- Related Questions & Answers
- How do you make a string in PHP with a backslash in it?
- How can I remove the ANSI escape sequences from a string in python?
- How to Split a String with Escaped Delimiters?
- How do I do a case insensitive string comparison in Python?
- How do I execute a string containing Python code in Python?
- How do I wrap a string in a file in Python?
- How to process escape sequences in a string in Python?
- How important is backslash in breaking a string in JavaScript?
- How do I format a string using a dictionary in Python 3?
- MySQL query to replace backslash from a varchar column with preceding backslash string values
- How do I serialize a Python dictionary into a string and vice versa?
- How do I remove a substring from the end of a string in Python?
- How do I create a Python namespace?
- How do I check if a string has alphabets or numbers in Python?
- How do I copy a file in python?
Previous Page Print Page Next Page
Advertisements
Sometimes, we want to un-escape a backslash-escaped string with Python.
In this article, we’ll look at how to un-escape a backslash-escaped string with Python.
How to un-escape a backslash-escaped string with Python?
To un-escape a backslash-escaped string with Python, we can use the string encode
and decode
method.
For instance, we write
s = my_string.encode['raw_unicode_escape'].decode['unicode_escape']
to call encode
with 'raw_unicode_escape'
to encode the string as an escaped string.
Then
we call decode
with 'unicode_escape'
to unescape the string.
Conclusion
To un-escape a backslash-escaped string with Python, we can use the string encode
and decode
method.
Web developer specializing in React, Vue, and front end development.
View Archive