This post will discuss how to convert bytes to a string in Python.

1. Using decode() function

The idea is to use the bytes.decode() function for decoding the given bytes to return a string. It takes the encoding used to encode the data to bytes. If no encoding is specified, the utf-8 encoding is applied.

Download  Run Code

2. Using String constructor

Alternatively, you can use the string constructor str(data, encoding) to get a string decoded from the given bytes using the given encoding.

Download  Run Code

3. Using map() function

You can get the string constructed from the bytes array by mapping each byte to a character using the chr function. Here’s what the code would look like:

Download  Run Code

That’s all about converting bytes to string in Python.