Unescape a backslash-escaped string in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Unescape a backslash-escaped string in Python #

To unescape a backslash-escaped string:

  1. Use the str.encode() method to convert the string to a bytes object with raw_unicode_escape codec.
  2. Use the bytes.decode() method to convert the bytes object to an unescaped string with the unicode_escape codec.
main.py
my_str = 'first\\nsecond\\nthird' result = my_str.encode('raw_unicode_escape').decode('unicode_escape') # first # second # third print(result)

The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

We used the raw_unicode_escape codec to convert the string to a bytes object without escaping the backslashes in any way.

main.py
my_str = 'first\\nsecond\\nthird' my_bytes = my_str.encode('raw_unicode_escape') print(my_bytes) # ๐Ÿ‘‰๏ธ b'first\\nsecond\\nthird'

The next step is to use the bytes.decode() method to convert the bytes object to an unescaped string.

main.py
my_str = 'first\\nsecond\\nthird' result = my_str.encode('raw_unicode_escape').decode('unicode_escape') # first # second # third print(result)

Here is a more verbose version of the code snippet above.

main.py
my_str = 'first\\nsecond\\nthird' result = my_str.encode( 'latin-1', errors='backslashreplace').decode('unicode_escape') print(result)

If an error occurs when encoding the string to a bytes object, we replace the characters that cannot be translated with a backslash-escaped sequence.

main.py
my_str = 'ั„ะธั€ัั‚\\nsecond\\nั‚ั…ะธั€ะด' # ๐Ÿ‘‡๏ธ b'\\u0444\\u0438\\u0440\\u0441\\u0442\\nsecond\\n\\u0442\\u0445\\u0438\\u0440\\u0434' print(my_str.encode('latin-1', errors='backslashreplace'))

The bytes.decode() method then converts the bytes object to an unescaped string.

The bytes.decode method returns a string decoded from the given bytes. The default encoding is utf-8.

This approach also works for strings containing non-ASCII characters.

main.py
my_str = 'ั„ะธั€ัั‚\\nsecond\\nั‚ั…ะธั€ะด' # ั„ะธั€ัั‚ # second # ั‚ั…ะธั€ะด result = my_str.encode('raw_unicode_escape').decode('unicode_escape') print(result)
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2022 Borislav Hadzhiev