Categories
Python Answers

How to un-escape a backslash-escaped string with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *