Python convert bytes to string

Total
0
Shares

If you want to convert bytes to string in Python, then you may use multiple ways of doing that. In this article, we are going to look at few methods.

Consider this example –

bytes_string = b'Ironman'

print(bytes_string.decode('utf-8'))

In this code, I am using decode function with utf-8 encoding to convert bytes of Ironman back to string.

Another way of doing that is using str() function with encoding. Check this code –

bytes_string = b'Ironman'

print(str(bytes_string, encoding))

If you have array of bytes then you may first convert them into characters and then join them together to make a string. Look at this code example –

>>> bytes_string = [73, 114, 111, 110, 109, 97, 110]
>>> print("".join(map(chr, bytes_data)))
Ironman

    Tweet this to help others

Live Demo

Open Live Demo