How do you unescape a string in python?

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




How do you unescape a string in python?

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Mohammad Nauman

More Detail

How do you unescape a string in python?

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

MANAS DASGUPTA

More Detail

How do you unescape a string in python?

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

How do you unescape a string in python?

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

How do you unescape a character in Python?

unescape() in Python. With the help of html. unescape() method, we can convert the ascii string into html script by replacing ascii characters with special characters by using html. escape() method.

How do I escape a character in a string?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do I remove an escape character in Python?

Practical Data Science using Python You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]

How do you escape HTML tags in Python?

Escape sequences in HTML always start with an ampersand and end with a semicolon. ... What is html. escape() in Python?.