32

In Python, I have a string like this:

'\\x89\\n'

How can I decode it into a normal string like:

'\x89\n'
martineau
  • 119,623
  • 25
  • 170
  • 301
Vince.Wu
  • 870
  • 1
  • 10
  • 17

2 Answers2

43

If your input value is a str string, use codecs.decode() to convert:

import codecs

codecs.decode(raw_unicode_string, 'unicode_escape')

If your input value is a bytes object, you can use the bytes.decode() method:

raw_byte_string.decode('unicode_escape')

Demo:

>>> import codecs
>>> codecs.decode('\\x89\\n', 'unicode_escape')
'\x89\n'
>>> b'\\x89\\n'.decode('unicode_escape')
'\x89\n'

Python 2 byte strings can be decoded with the 'string_escape' codec:

>>> import sys; sys.version_info[:2]
(2, 7)
>>> '\\x89\\n'.decode('string_escape')
'\x89\n'

For Unicode literals (with a u prefix, e.g. u'\\x89\\n'), use 'unicode_escape'.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 5
    This doesn't work in python3. (`str` doesn't have a `decode` method and also `string_escape` isn't a valid encoding anymore). – Bakuriu Jun 16 '14 at 11:33
  • @Bakuriu: The OP *never specified a Python version*. – Martijn Pieters Jun 16 '14 at 11:40
  • Yeah, you're right, what I was trying to decode is a bytes class object, not a str object! – Vince.Wu Jun 16 '14 at 11:49
  • @user486005: that was crucial information; I had to assume you were using Python 2 (still the more likely option, statistically speaking). I've updated my answer to include the correct methods to use in Python 3. – Martijn Pieters Jun 16 '14 at 11:50
  • @MartijnPieters I didn't know that python-3 has remove the decode method of string object then, at first I thought it was a string object while it was a bytes object indeed, so it just happens to run successfully. Thanks a lot. – Vince.Wu Jun 16 '14 at 12:08
  • I'm using python3. I have a variable that I get by reference. When I try to do the conversion of my variable it doesn't work. Any ideas? Example: my_string = 'C:\Program Files\Java\jre1.8.0_202\bin\java.exe' codecs.decode(my_string, 'unicode_escape') # 'C:\\Program Files\\Java\\jre1.8.0_202\x08in\\java.exe' – grand Feb 14 '19 at 14:17
  • @grand: you don't have any escapes in that string. Echoing a string with backslashes always shows valid, copyable Python syntax. See [Why do backslashes appear twice?](//stackoverflow.com/q/24085680) – Martijn Pieters Feb 14 '19 at 15:45
  • @MartijnPieters, I am trying same thing in python3. seems decode function is not available with str. how do we convert raw strings to normal strings in latest python3? – sandeep nagendra Aug 07 '20 at 07:08
  • name = "sandeep\tnagendra\n" print(name) --> this gives tab and newline print("%r" % name) --> THis converts normal string to raw string name = r"sandeep\tnagendr\n" print(name) --> This prints raw string Now how do we change raw string back to normal one? – sandeep nagendra Aug 07 '20 at 08:11
  • @sandeepnagendra please read the whole answer, there is a Python 3 section. Your first expression produces a string *representation* (what `repr()` returns), which includes quotes. – Martijn Pieters Aug 07 '20 at 10:05
12

This would work for Python 3:

b'\\x89\\n'.decode('unicode_escape')
Ivan Kucerak
  • 591
  • 4
  • 6