How to Convert Bytes to String in Python

To convert bytes to string in Python, you can use the decode() function.

The following example shows how to convert bytes to string in Python using the decode() function.

Using decode() Function

We can use the decode() function to convert bytes to a string.

Suppose we have the following bytes object:

# Create a bytes object
byte_data = b'Hello World!'

# Convert bytes to string
str_data = byte_data.decode('UTF-8')

# Print the string data
print(str_data)

Output: 👇️

Hello World!

In this example, we use the decode() function to convert the bytes object byte_data to a string str_data. The output shows the string converted from bytes using the decode() function.

Conclusion

We can use the decode() function to convert bytes to a string in Python. This method provides a convenient way to handle byte-to-string conversions.