Convert bytes to a string Convert bytes to a string python-3.x python-3.x

Convert bytes to a string


You need to decode the bytes object to produce a string:

>>> b"abcde"b'abcde'# utf-8 is used here because it is a very common encoding, but you# need to use the encoding your data is actually in.>>> b"abcde".decode("utf-8") 'abcde'


You need to decode the byte string and turn it in to a character (Unicode) string.

On Python 2

encoding = 'utf-8''hello'.decode(encoding)

or

unicode('hello', encoding)

On Python 3

encoding = 'utf-8'b'hello'.decode(encoding)

or

str(b'hello', encoding)


I think this way is easy:

>>> bytes_data = [112, 52, 52]>>> "".join(map(chr, bytes_data))'p44'