Python Program to Convert Bytes to a String
ⓘ Sponsored by 10xer.co
Converting bytes to a string involves decoding the bytes object into a string object. Bytes objects represent sequences of bytes, while string objects represent sequences of Unicode characters.
Python Code :
The below Python program converts bytes to a string:
bytes_data = b'Hello, world!'
# Use the decode() method to convert bytes to string
string_data = bytes_data.decode('utf-8')
# Print the string data
print("String data:", string_data)
This program defines a bytes variable bytes_data with the value b’Hello, world!'.
To convert the bytes to a string, the program uses the decode() method. The decode() method converts the bytes object to a string using the specified encoding. In this case, we use the UTF-8 encoding.
Finally, the program prints the string data using the print() function. The output will be String data: Hello, world!.
ⓘ Sponsored by 10xer.co