Data types define how the data will be and what the memory is asked by the interpreter to hold. It is equally essential to understand the data type because they determine what operations a programmer can perform on specific data. Python considers everything as an object. Therefore, every data type comes under any class. The variables we create are objects of these classes. But there are situations where we have to convert a variable's data type from one form to another. In this article, you will learn about how to convert bytes to strings.
Method 1: Using str() function:
The str() function in Python helps in returning the string version of any object. its syntax is:
str(python_object, encoding_scheme='utf-8', errors = 'strict')
It takes three parameters. The first is the python_object that we have to pass to get its string representation. The next parameter is the encoding_scheme. It tells the program about the encoding scheme of the object given with it. The third parameter is the 'errors'. It defines the type of error that will occur when decoding will fail.
Program:
valu = b'Karlos Ray'
print('\n Given Input is:')
print(valu)
# if you check the type, you will find it in Bytes
print(type(valu))
op = str(valu, 'UTF-8')
print('\n The Given Output is:')
print(op)
print(type(op))
Output:
Given Input is:
b'Karlos Ray'
<class 'bytes'>
The Given Output is:
Karlos Ray
<class 'str'>
Explanation:
In this program, we have taken a byte string by writing a string and prefixed it with a b. Then, we are printing the string variable. Next we are checking its type using the type() function before converting it to a string format. Now, we use the str() function that is taking 2 parameters. The first parameter is the byte object whose type we want to change. The second parameter talks about the encoding scheme in which we want to convert the byte value. We are then storing the converted value in another variable name ‘op’. Finally, we are printing the converted value and its type.
Method 2: Using the decode() method:
As the name suggests, decode() is another popular method that converts an encoding scheme into another desired encoding scheme. When the argument is passed as a string, it converts that to another specified encoding scheme. It works just the opposite of the encode() method.
The syntax is:
decode(encoding, error)
If we input the data as a byte object and then use the decode(), this method will convert its schemes and return the output in string format.
Program:
valu = b'Karlos Ray'
print('\n Given Input is:')
print(valu)
# if you check the type, you will find it in Bytes
print(type(valu))
op = valu.decode()
print('\n The Given Output is:')
print(op)
print(type(op))
Output:
Given Input is:
b'Karlos Ray'
<class 'bytes'>
The Given Output is:
Karlos Ray
<class 'str'>
Explanation:
In this program, we have taken a byte string by writing a string and prefixed it with a b. Then, we are printing the string variable. Next we are checking its type using the type() function before converting it to a string format. Now, we use the byte_object.decode() method. It does not take any parameter. We are then storing the converted value in another variable name ‘op’. Finally, we are printing the converted value and its type.
Method 3: Using codecs.decode() method:
The codecs.decode() method is specially designed to convert any binary string / bytes-based string value directly to a normal string format. It returns a decoded string and accepts the bytes object as an argument during the method call.
The syntax is
codecs.decode(byte_string)
Program:
import codecs
valu = b'Karlos Ray'
print('\n Given Input is:')
print(valu)
# if you check the type, you will find it in Bytes
print(type(valu))
op = codecs.decode(valu)
print('\n The Given Output is:')
print(op)
print(type(op))
Output:
b'Karlos Ray'
<class 'bytes'>
The Given Output is:
Karlos Ray
<class 'str'>
Explanation:
In this program, we have taken a byte string by writing a string and prefixed it with a b. Then, we are printing the string variable. Next we are checking its type using the type() function before converting it to a string format. Now, we use the codecs.decode() method that is taking 1 parameters. The parameter is the byte object whose type we want to change. We are then storing the converted value in another variable name ‘op’. Finally, we are printing the converted value and its type.
Conclusion:
Among all these three techniques of converting Python bytes to strings, the str() is the most common and well-established technique. It is because every programmer knows this method if they have encountered string handling in Python. But decode() works faster because it directly performs encoding operations. codecs.decode() might work slow in many situations as to use this method, programmers need to import a separate module in the program.