Home Python How to convert bytes to a string in Python

How to convert bytes to a string in Python

Data types are the classification or categorizing of data elements. The latter denotes the kind of value that specifies which operations are performed on a given data set. Data types are classes, and variables are instances (objects) of these classes because everything in Python programming is an object.

Before exploring more into this topic, we must first grasp the fundamentals of the Byte data type.

Python’s Byte Data Type

You’re probably already aware of the byte data type if you’re familiar with Python. However, if someone is unfamiliar with Python, we will explain the notion. Let’s examine the following example.

string_one = 'Codeunderscored bytes to string conversion'
print(type(string_one))
string_two = b'Codeunderscored bytes to string conversion'
print(type(string_two))

Two strings with the same value are defined. Although the values appear to be the same, the data types are not. The first string variable is the string data type, but the second is the byte data type. The second string variable is prefixed with ‘b,’ indicating generating byte data rather than str data.

We can differentiate these data types as follows:

  • A string can be advanced as a collection of Unicode characters (encoded in UTF-16 or UTF-32, depending on the Python compiler).
  • Bytes or the byte – A byte is an integer that ranges from 0 to 255 and is represented by the letters ‘b’ or ‘B.’ By writing ‘b literal in front of a standard string, we can convert any string to bytes.

The Main Difference Between Strings and Bytes

In Python 2.x, both str and bytes data types are utilized as Byte type objects, but this is not the case in Python 3.x. Bytes and strings differ in that strings are easy to read or human-readable, whereas bytes are eventually machine-readable, and the string is also transformed to byte before processing.

A byte data type is directly saved on the disk when declared in Python, whereas a string is transformed to a byte and then stored on the disk. In addition, Bytes represent low-level binary data structures, whereas Strings represent characters, words, or sentences.

How to convert bytes to a string in Python

The following methods are used to convert bytes to strings:

Making use of the str() method

The str() method is another simple approach to convert bytes to strings. You must provide the correct encoding to this method; else, the conversion will be erroneous. Python’s str() method returns the object’s string representation.

# conversion of bytes into the string using the method decode()
dataVal = b'CodeUnderscored'

# showing input data
print('\nInput data :')
print(dataVal)
print(type(dataVal))

# the conversion process
outputVal = str(dataVal, 'UTF-8')

# showing the output value
print('\nOutput data:')
print(outputVal)
print(type(outputVal))

Example: converting bytes to string using str() in Python

dataVal = b'CodeUnderscoredCode \xf0\x9f\x8d\x95!'
print(dataVal)
print("Output before the conversion type is:", type(dataVal))

# converting from bytes to string
outputVal = str(dataVal,'UTF-8')
print(outputVal)
print("Output after the conversion type is: ", type(outputVal))

How to use the decode() method?

In Python, the codecs module is a standard built-in module with a decode() method that takes input bytes and outputs a string as output data.

This method converts from one encoding scheme to another by encoding the argument string in the desired encoding scheme. It has the opposite effect as encoding. The decode() method is available in the bytes class. It transforms the byte object into a string. If you don’t indicate anything, it defaults to UTF-8 encoding. The goal is to make use of the bytes.decode() to return a string after decoding the given bytes. The last-mentioned converts the data to bytes using encoding.

# converting bytes into the string using the method decode()

dataVal = b'CodeUnderscored'

#how to display the input
print('\nInput data:')
print(dataVal)
print(type(dataVal))

# the conversion process
outputVal = dataVal.decode()

# finally showing the output
print('\nOutput data:')
print(outputVal)
print(type(outputVal))

Example: converting bytes to string using decode() in Python

dataVal = b'CodeUnderscoredCode \xf0\x9f\x8d\x95!'
print(dataVal)
print("Output before the conversion type is: ", type(dataVal))

# converting from from bytes to string
outputVal = dataVal.decode()
print(outputVal)
print("Output after the conversion type is: ", type(outputVal))

Using the method codecs.decode()

This method is used to convert a binary string to a human-readable format.

# conversion of bytes to string using the method decode()

# importing the necessary modules
import codecs

dataVal = b'CodeUnderscored

# showing the input data
print('\nInput Val:')
print(dataVal)
print(type(dataVal))

# the conversion process
outputVal = codecs.decode(dataVal)

# showing the output data
print('\nOutput data:')
print(outputVal)
print(type(outputVal))

Using the map() function

Using the chr function to convert each byte to a character may get the string created from the bytes array. It is how the code would look:

if __name__ == '__main__':
 
    bytes_data_info = [92, 94, 96, 98]
 
    resultant_string = ''.join(map(chr, bytes_data_info))
    print(resultant_string)

bytearray to string conversion using bytes() function

We can use Python’s built-in bytes() function to convert a bytearray containing string characters with the utf-8 encoding into a string variable if we have a bytearray containing string characters with the utf-8 encoding.

Bytes() returns an immutable bytes object, which you can place in a string variable. The bytes() function converts a bytearray to a string in the following code snippet.

byteVal = bytearray("Codeunderscored", encoding="utf-8")
stringVal = bytes(byteVal)
print(stringVal)

We started by encoding the text ‘Codeunderscored’ with a utf-8 encoding within a bytearray object. The bytes() function in the code above transformed the bytearray object byteVal into a string variable stringVal. We used the bytes() function to convert the bytearray to a string, then put in the string variable stringVal.

Finally, we printed the contents contained within the stringVal variable. The output reveals that this technique starts our original data with a byteVal object and then encloses it in single quotes. This issue is handled in the method that follows.

bytearray to string conversion using bytearray.decode()

As we can see, the bytes() function turns a bytearray into a string while also adding additional data to the original string. String manipulation can overcome this problem; however, it is time-consuming. It is handled automatically by the bytearray.decode() function. The data initially encoded inside the bytearray is decoded using this method.

The bytearray.decode() function converts a bytearray to a string in the following code snippet.

byteVal = bytearray("test", encoding="utf-8")
stringVal = byteVal.decode()
print(stringVal)

The byteVal.decode() function in the code above transformed the bytearray object byteVal into a string variable stringVal. We started by encoding the text test with a utf-8 encoding within a bytearray object. The bytearray was then transformed to a string using the byteVal.decode() function, and the saved output in the string variable stringVal. Finally, we printed the contents contained within the stringVal variable. This technique does not add any new data to our already encoded material, as evidenced by the output.

The bytearray.decode() method is preferable to the byte() method for converting a bytearray object into a string variable, as seen in the examples above.

Conclusion

This article has explored how to convert Bytes to a String using a Python script. We’ve also looked at making the most of the various data kinds. Because Python 2 is no longer in use, we’ve included Python 3 approach in the examples covered in the tutorial.

In addition, we have also looked at how to use the byte data type. The numerous methods for converting byte data type to string have been defined. Encoding and decoding are accomplished by using charsets, encoding, and binary.

You may also like

Leave a Comment