Convert Byte Literals to Python Strings



To convert byte literals to Python strings, you need to decode the bytes. It can be done using the decode method on the bytes object. 

 example

>>> b"abcde".decode("utf-8")
u'abcde'

You can also map bytes to chr if the bytes represent ASCII encoding as follows −

bytes = [112, 52, 52]

print("".join(map(chr, bytes)))

Output

p44
Updated on: 2020-02-13T10:32:41+05:30

430 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements