Converting bytes to a string in Python

Converting bytes to a string in Python programming is easy. You have to use either the bytes.decode or the bytearray.decode option. Here is a code sample for using the decode option to convert bytes or byte array to a string.

Sample

## Converting bytes to string
bytValue = b'hello there'
print(bytValue)

strResult = bytValue.decode()
print(strResult)

## Converting bytearray to string
bytArray = bytearray('hello, how are you', 'utf-8')
print(bytArray)

strResult = bytArray.decode()
print(strResult)
Converting bytes to a string in Python

Reference


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.