Convert escaped string back to original string

Question:

I have a string that looks like this:

>>> st = 'aaaaax12bbbbb'

I can convert it to a raw string via:

>>> escaped_st = st.encode('string-escape')
'aaaaa\x12bbbbb'

How can I convert the escaped string back to the original string? I was trying to do something like this:

escaped_st.replace('\\', '\')
Asked By: turtle

||

Answers:

Decode the encoded string with the same encoding:

>>> st = 'aaaaax12bbbbb'
>>> escaped_st = st.encode('string-escape')
>>> escaped_st
'aaaaa\x12bbbbb'
>>> escaped_st.decode('string-escape')
'aaaaax12bbbbb'
Answered By: falsetru
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.