Updated March 31, 2023
Introduction to Byte to String Python
In this article, we will see how to convert the byte to string in Python. In general, one byte is defined as part of a memory location that can hold size of 8 bits, and a string in Python is defined as a sequence of bytes representing Unicode characters. As all programming languages consider the conversion of byte to string and vice versa using encoding and decoding methods in their respectively. In Python programming language also provide encode() and decode() methods. To convert the byte to the string, we have to use the decode() method and to convert from string to byte; we have to use encode(). Therefore, the encoding process and decoding process both are inverse process.
Working of Conversion of Byte to String in Python with Examples
In this article, we will see how to convert the byte to string using Python. Byte objects are usually not human-readable, but they are machine-readable, and these byte objects contain an array of bytes. In general, we can say string as an array of characters, and they are human-readable forms. Therefore, whenever we need to store a byte on the disk or memory, we can directly store it as it is machine-readable, but we need to store it in the form of a string it needs to be decoded before storing it on the disk.
Overall, we can store machine-readable objects directly on a disk, and when we need to store it as a human-readable string on the disk, byte objects must be decoded to string to store it directly on the disk. Therefore, to convert from byte to string, use decode() function and conversion from string to byte, we need to use encode() function. Both these functions accept an argument that encodes Unicode character, and the default for both these functions is UTF-8.
First, we will see how we convert the given string to byte and back byte to string with the examples below.
Code:
print("Program to convert given string to byte")
print("\n")
print("The given string is as follows:")
str_lt = ['Educba', 'Training']
print(str(str_lt))
print("\n")
r = []
for e in str_lt:
r.extend(ord(num) for num in e)
print("The byte value in ascii is : " + str(r))
Output:
In the above program, we can see the given string is converted to bytes in the encoding Unicode character ASCII values.
Now let us see how to convert it back from these bytes to string.
Code:
print("Program to demonstrate conversion of bytes to string ")
print("\n")
bt_lt = [69, 100, 117, 99, 98, 97, 84, 114, 97, 105, 110, 105, 110, 103]
print("The byte values to convert it to string is as follows")
print (bt_lt)
print('\n')
r = ""
for v in bt_lt:
r = r + chr(v)
print ("The given byte is converted to string")
print(str(r))
Output:
So now, in the above example, we can see the same bytes which we obtained in the above program will be converted to string back.
Now let us demonstrate how to check if the conversion of byte to string is done in Python in the below example:
Code:
print("Program to demonstrate Byte Decoding to string")
print("\n")
print("The given string for checking decoding is as follows")
st = 'Educba Training'
print(st)
print("\n")
print("The string is converted to bytes using b")
bt = b'Educba Training'
print(bt)
print("\n")
print("The decoded string from byte is:")
ot = bt.decode('utf-8')
print(ot)
print("\n")
print("Checking if given byte is converted to the given string:")
if (ot==st):
print ("Decoding successful")
else : print ("Decoding Unsuccessful")
Output:
In the above program, we can see the given string is “Educba Training”, and we are storing the byte value of the given string in another variable “bt”, which is later decoded to a string using the decode() function and we are using encoding Unicode character specification of ‘UTF-8’, and the decoded value is stored in another variable “ot” which will be in string format. Therefore to check if the decoded value is similar to the given string value, then it will print the decoding is successful, as we can see in the above screenshot where we can ensure that the bytes are converted to a string.
Conclusion
In this article, we conclude that the conversion of byte to string or vice versa. In this article, we have seen that Python provides two methods: the encode() function to convert string to byte and the decode() function to convert the byte to string. We first saw how to convert string to bytes in python and byte to string with a simple Python program. Then we saw the real python program that we used to decode() function for converting a byte to string using encoding Unicode character “UTF-8” there are many Unicode characters such as ASCII, utf-8, WAV, etc.
Recommended Articles
We hope that this EDUCBA information on “Byte to String Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.