How do I un-escape a backslash-escaped string in Python?


In Python, you can un-escape a backslash-escaped string using the decode() method or by using the ast module. Here are few different ways to do it:

Using decode() method

Example

In this method, we define a string called escaped_string that contains a backslash-escaped newline character \n. We convert this string to bytes using the bytes() function and specify the utf-8 encoding. Then, we use the decode() method with the unicode_escape codec to un-escape the backslash-escaped characters in the string. Finally, we print out the resulting un-escaped string.

escaped_string = 'This is a backslash-escaped string: \n foo ' 
unescaped_string = bytes(escaped_string, 'utf-8').decode('unicode_escape')
print(unescaped_string)

Output

This is a backslash-escaped string: 
 foo

Using the ast module

Example

In this method, we import the ast module, which provides a function called literal_eval() that can evaluate a string containing a Python literal expression, such as a string literal. We define a string called escaped_string that contains a backslash-escaped newline character \n. We use an f-string to wrap this string in double quotes and pass it to literal_eval() as a string literal. This causes literal_eval() to evaluate the string as a Python string literal, effectively un-escaping the backslash-escaped characters. Finally, we print out the resulting un-escaped string.

import ast
escaped_string = 'This is a backslash-escaped string: \n foo'
unescaped_string = ast.literal_eval(f'"{escaped_string}"')

print(unescaped_string)

Output

This is a backslash-escaped string:
foo

Using replace() method

Example

In this method, we define a string called escaped_string that contains a backslash-escaped newline character \n. We use the replace() method to replace double backslashes \ with single backslashes \, effectively un-escaping the backslash-escaped characters. Finally, we print out the resulting un-escaped string.

escaped_string = 'This is a backslash-escaped string: \n'
unescaped_string = escaped_string.replace('', '')

print(unescaped_string)

Output

This is a backslash-escaped string: \n

Here are some more ways to un-escape a backslash-escaped string in Python:

Using the re module

Example

In this method, we import the re module, which provides regular expression matching functionality. We define a string called escaped_string that contains a backslash-escaped newline character \n. The first argument to re.sub() is a regular expression pattern that matches the backslash-escaped newline character (\n). The second argument is the replacement string, which is simply the newline character (\n). The third argument is the string to perform the substitution on (escaped_string).

The resulting string after substitution is assigned to the unescaped_string variable. This variable contains the same characters as escaped_string, but with the backslash-escaped newline character replaced with an actual newline character. Finally, we print out the resulting un-escaped string.

import re

escaped_string = 'This is a backslash-escaped string: \n foo'
unescaped_string = re.sub(r'\n', r'\n', escaped_string)

print(unescaped_string)

Output

This is a backslash-escaped string: 
foo

Using eval() function

Example

In this method, we use the eval() function to evaluate a string as a Python expression. We define a string called escaped_string that contains a backslash-escaped newline character \n. We use an f-string to wrap this string in double quotes and pass it to eval() as a string literal. This causes eval() to evaluate the string as a Python string literal, effectively un-escaping the backslash-escaped characters. Finally, we print out the resulting un-escaped string.

It is to be noted that this method is generally not recommended as it can be unsafe if the input string comes from an untrusted source, as it allows for arbitrary code execution.

escaped_string = 'This is a backslash-escaped string: \n foo'
unescaped_string = eval(f'"{escaped_string}"')

print(unescaped_string)

Output

This is a backslash-escaped string:
foo

Using unicode_escape encoding

Example

In this code, escaped_string is first defined as a string containing a backslash-escaped newline character (\n).

The encode() method is called on escaped_string with no arguments, which converts it to a bytes object. The decode() method is then called on the bytes object with the unicode_escape encoding specified as an argument. This converts the bytes object back to a Unicode string with the escape sequences replaced by their corresponding Unicode characters.

Finally, the unescaped string is printed to the console.

escaped_string = 'This is a backslash-escaped string: \n foo'
unescaped_string = escaped_string.encode().decode('unicode_escape')

print(unescaped_string)

Output

This is a backslash-escaped string:
foo

In short, hopefully these additional examples have helped you un-escape your backslash-escaped strings in Python!

Updated on: 10-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements