Skip to content
Home » How to Convert Bytes to String in Python

How to Convert Bytes to String in Python

In this tutorial let’s learn about How to Convert Bytes to String in Python. This task will vary depending on the version of Python you’re running. Although Python 2 is no longer supported, many projects still use it, therefore we’ll cover both the Python 2 and Python 3 ways.

Bytestrings are technically known as bytes in Python 3, and they are an immutable series of integers in the range 0 = x 256. The bytearray, which is similar to bytes but it is mutable which is another bytes-like object added in 2.6.

Convert Bytes to String with decode() Method Python

The bytes .decode() method could transform bytes to strings using the specified encoding scheme. In most circumstances, leaving the encoding method at its default utf-8, is safe but it is not always safe because the bytes could be encoded with other encoding techniques instead of utf-8.

Code Example :

byte_data = b"Alphabets : \x61\x62\x63"

print('Input data:',byte_data)
print(type(byte_data))

# decode() method
output_data = byte_data.decode()

print('Output:', output_data)
print(type(output_data))

Output :

Input data: b’Alphabets : abc’
<class ‘bytes’>

Output: Alphabets : abc
<class ‘str’>

Convert Bytes to String with str() Method Python

The str() function takes a variety of values and convert them to strings.

Code Example :

import codecs
byte_data = b"Alphabets : \x61\x62\x63"

# str() method
output_data = str(byte_data, 'UTF-8')

print(output_data)
print(type(output_data))

Output :

Alphabets : abc
<class ‘str’>

Convert Bytes to String with codecs Module in Python

The codecs module includes a set of base classes that describe the interface and may be used to simply construct your own Python codecs. To be used as a codec in Python, each codec must specify four interfaces: stateless encoder, stateless decoder, stream reader, and stream writer.

This function is used to convert a binary string into a normal string. Here Although you are not need to pass in the encoding argument, it is recommended that you do.

Code Example :

import codecs
byte_data = b"Alphabets : \x61\x62\x63"

# codecs.decode() method
output_data = codecs.decode(byte_data,'UTF-8')

print(output_data)
print(type(output_data))

Output :

Alphabets : abc
<class ‘str’>

Convert Bytes to String using chr() in Python

chr(i) returns a Unicode string with ordinal of one character. It could convert a byte element to a string but not the entire byte. We could use list comprehension or map to retrieve the converted string of bytes, whereas chr would be used to get the individual element.

Code Example :

byte_data = b"Alphabets : \x61\x62\x63"

# chr() method
output_data = "".join([chr(i) for i in byte_data])

print(output_data)
print(type(output_data))

Output :

Alphabets : abc
<class ‘str’>

Conclusion

In this tutorial we learnt different ways to convert the bytes into string in Python. We used str(), decode(), chr() and codecs methods and gone through the examples.

Similar Posts:

How to Convert a List to String in Python
Check if a String Contains Substring in Python