Google News
logo
Python Program to Convert Bytes to a String
To convert bytes to a string, you can use the `decode()` method in Python. Here's an example code that demonstrates how to convert bytes to a string :
Program :
bytes_string = b'Hello, World!'
string = bytes_string.decode('utf-8')
print(string)


In this code, we first define a bytes object `bytes_string` containing the byte representation of the string "Hello, World!". We then use the `decode()` method to convert the bytes to a string, using the `utf-8` encoding.

Finally, we print the resulting string using the `print()` function.

Output :
Hello, World!
This code demonstrates how to convert bytes to a string using the `decode()` method. Note that you need to specify the correct encoding when calling the `decode()` method, otherwise you may get an error or unexpected results.