Python

Bytes to String in Python

In Python, whenever we store anything, it will store it as a byte. The bytes are not human-readable, and strings are human-readable forms. Whenever we store any string, it will not directly store it as a string; it will be encoded into the bytes using different methods like ASCII and UTF-8.

For example, ‘I am a linuxhint’.encode (‘ASCII’)

In the above example, we convert the string into bytes using the encode method ASCII. And when we print it using the python print method, we will see results like b’I am a linuxhint’. Here we can see that the string just follows one character, b. In actuality, we are not able to read the bytes, but here the python print function decodes the bytes to human-readable forms so that we can read the strings.

But, in actuality, when we run this string to see each character of the bytes strings, it will print like this:

str1 = 'I am a linuxhint'
print(type(str1))
str2 = b'I am a linuxhint'
print(type(str2))

We created two strings. One string is straightforward. And the other string is bytes. When we print both string types, then we get results as shown below. The below results show that one string is a bytes type and one other is a string type.

<class 'str'>
<class 'bytes'>

Now, we will print each character of both the strings to find the difference between them. First, we will print a string (str1) using a for loop, which is a normal string.

for a in str1:
     print(a)

Output:

I

a
m

a

l
i
n
u
x
h
i
n
t

Now, we are going to print another string (str2) which is a byte type.

for a in str2:
     print(a)

Output:

73
32
97
109
32
97
32
108
105
110
117
120
104
105
110
116

From the above results, we can see the differences between the bytes type and the string type. Bytes type are not human-readable forms.

So, now we are going to see different methods which can convert the bytes to strings.

Method 1: Using the map() function

In this method, we will use the map () function to convert the bytes to a string format. The below small program will clear the concept.

byte = [97, 99, 100]

s = ''.join(map(chr, byte))
print(s)

Output:

acd

Line 1: We created a list of name byte.

Line 3: We then passed the characters of the element in the list as a parameter and a byte as a parameter to the join() function, which will join all the characters after conversion. Finally, the result is printed.

Method 2: Using decode () function

Another method is a decode () function. The decode function works just opposite of the encode () function.

#convert bytes to string using decode()

str = b'blogs linuxhint'
print(str)
print(type(str))

# now converting bytes to string
output = str.decode()

print('\nOutput:')
print(output)
print(type(output))

Output:

b'blogs linuxhint'
<class 'bytes'>

Output:

blogs linuxhint
<class 'str'>

Line 3: We have created a byte string.

Line 4 to 5: These lines just print information about the string we created at line 3.

Line 8: Now, we call the decode function and save the result to a new variable name.

Line 11 to 12: These lines show that now our string has now no longer byte type and that we can confirm the type of the output, which is a string type.

Method 3: Using the codecs.decode () function

In this method, we are going to use codecs.decode () function. This function is used to convert the binary string into normal forms. So let’s see how this function actually works.

#convert bytes to string using codecs()
import codecs
str = b'blogs linuxhint'
print(str)
print(type(str))

# now converting bytes to string
output = codecs.decode(str)

print('\nOutput:')
print(output)
print(type(output))

Output:

b'blogs linuxhint'
<class 'bytes'>

Output:

blogs linuxhint
<class 'str'>

Line 2:  We import the library codecs.

Line 3: We have created a byte string.

Line 4 to 5: These lines just print information about the string we created at line 3.

Line 8: Now we are calling the caodecs.decode function and saving the result to a new variable name.

Line 11 to 12: These lines show that now our string has no longer of byte type, and we can confirm the type of the output, which is a string type.

Method 4: Using the str () function

We can also convert the bytes to normal strings using the str () function. The small program to understand this method is given below.

if __name__ == '__main__':
        str1 = b'blogs linuxhint'
        print(str)
        print(type(str))
        # now converting bytes to string
        output = str(str1, 'utf-8')
        print('\nOutput:')
        print(output)
        print(type(output))

Output:

<class 'str'>
<class 'type'>

Output:

blogs linuxhint
<class 'str'>

Line 2: We have created a byte string.

Line 3 to 4: These lines just print information about the string we created at line 3.

Line 6: Now, we call the str () function and save the result to a new variable name.

Line 7 to 9: These lines show that now our string has no longer byte type, and we can confirm the type of the output, which is a string type.

Conclusion

As python programmers, we work on different languages daily, and sometimes we get an error due to the bytes. So in this article, we are trying to give some methods how to convert the bytes to string so that when we apply any methods related to the string, you will not get any error.

In this tutorial, we have given all the concepts which we can use to convert the bytes to strings. You can choose according to your program requirements.

About the author

Shekhar Pandey