How to Convert Bytes to a String in Python

A Byte object is a basic unit of digital information storage and processing that can be stored inside a disc and can be read by a machine. However, if a human needs to read the data, it is required to convert the byte data into a human-readable format which is a string/text. This article discusses different methods to convert bytes to a string and illustrates each method with an example. 

How to Convert Bytes to a String in Python?

Before diving into how bytes data can be converted to a string in Python, let us understand the concept of bytes. A byte is a digital information that has a length of 8 bits.

Byte is a built-in data type in Python that indicates the sequence of bytes which can store the data. The data can be any textual or visual information or even binary data. Bytes can also represent emojis and other characters that do not particularly belong to ASCII.

The process of converting bytes into a string to get more human-readable data is called decoding while if the data is in the string format and it has to be stored in a disc, it is converted to bytes so that a machine can read it. This process is called encoding. The visual diagram below displays the decoding and encoding processes along with the Python functions that perform these processes:

The well-known approaches for bytes to string conversion are listed below:

  1. Convert Bytes to String Using Bytes Class in Python
  2. Convert Bytes to String Using decode() Method
  3. Convert Bytes to String Using str Constructor
  4. Convert Bytes to String Using Pandas Library
  5. Convert Bytes to String Using Codecs Module

Let us discuss each method in detail.

Method 1: Convert Bytes to String Using Bytes Class in Python

The bytes() constructor in Python can create a new Python object that takes an iterator and gives back the provided data in bytes format. It can be converted to a string by implementing either str() or decode() function. This holds a lot of value when working with binary data. The following code explains how bytes can be converted to string using bytes() and decode() functionalities:

text = "I love to code in Python."
create_bytes = bytes(text, 'utf-8')
print("Input data from user: ", create_bytes)
print("The type of input data is: ", type(create_bytes))
dc_str = create_bytes.decode('utf-8')
print("\nThe output will be:", dc_str)
print("The type of the output will be",type(dc_str))

In the above code,

  • A string text is defined with a value of “I love to code in Python”.
  • The string/text is converted to bytes via the bytes() function of the bytes class. Inside the bytes() function, “text” and “utf-8” are passed as arguments. 
  • The encoding system utf-8 encodes the characters and the returned value will be stored in the variable named create_bytes.
  • The type of the create_bytes variable is checked using the type() function. The class will be bytes.
  • The decode function is called on create_bytes with utf-8 as its parameter value. 
  • The result is stored in the variable dc_str which represents the decoded string. The variable dc_str along with its data type is printed.

Output

The following output shows how bytes can be converted to a string in Python using the bytes class:

Method 2: Convert Bytes to String Using decode() Method

The decode() method is used when handling the textual information that is encoded in a particular character encoding like ASCII or UTF-16.  It takes encoded data and gives back the decoded string. The following code explains how the decide() method can be used to convert bytes into string format in Python:

byte_data = b"I love to code in Python."
print("User input: ", byte_data)
print("The class type of user input is: " ,type(byte_data))
dc_str = byte_data.decode()
print("\nAfter the conversion" ,dc_str)
print("The type of the converted data is: ",type(dc_str))

In the above code,

  • The user input is stored in the byte_data. Adding a “b” keyword before a string will change the string value to bytes.
  • The user input and its type are printed using type(). The class of the user input will be bytes.
  • A function named decode() is called on the byte_data and its result is stored in the variable dc_str. The variable contained the string value of the input in bytes.
  • The dc_str is printed along with its type which is a string.

Output

The below output snippet shows how bytes can be converted to a string using the decode() function:

Method 3: Convert Bytes to String Using str Constructor

Another method to convert the bytes into a string is using the str constructor. This is helpful when data from a file has to be read. The following code explains how using the str constructor can convert the bytes into string in Python:

byte_data = b"Coding solve problems!"
print("User input: ", byte_data)
print("The class type of user input is: " ,type(byte_data))
text = str(byte_data, 'UTF-8')
print("\nAfter the conversion: " ,text)
print("The type of the converted data is: ",type(text))

In the above code,

  • The byte_data contains the bytes input: b”Coding solve problems!”.
  • The byte_data which is the user input is printed along with its type which is bytes.
  • To convert it into a string, the str() function takes the byte_data and ‘utf-8’ as a parameter and store its value in the text which will be a string
  • The variable text along with its type(string) is printed.

Output

The following output displays how bytes can be converted to a string value using str() in Python:

Method 4: Convert Bytes to String Using Pandas Library

Another way to convert the bytes into a string is using functionalities from the Pandas library. Using the str.decode function from Pandas library can do the conversion. The following code explains how bytes can be converted to string using the str.decode method of the Pandas library:

import pandas as pd
byte_dict = {'planets' : [ b'Venus', b'Mars', b'Saturn', b'Uranus']}
df = pd.DataFrame(data=byte_dict)
str_data = df['planets'].str.decode("utf-8")
print(str_data)

In the above code,

  • Pandas library is imported as pd.
  • A dictionary byte_dict is defined which has a single key-value pair. The key is “planets” which contain a list of byte values representing different planets.
  • The pd.DataFrame is called on byte_dict which will convert it into a data frame having a single column planets that contains the byte strings.
  • The planets column of the data frame is accessed and the str.decode() method with ‘utf-8’ as its parameter is called to convert each of the byte values to the string.

Output

The following output displays how bytes can be converted to string using the Pandas library in Python:

Method 5: Convert Bytes to String Using Codecs Module

The codecs module has a collection of primary classes that offer interfaces and streams for transcoding the data into code. We can work effectively with the codecs objects using the codecs module. The following code explains how the bytes can be converted to string using the decode() method from the codecs module:

import codecs
byte_data = b'Coding is fun!'
print("User input: ", byte_data)
print("The class type of user input is: " ,type(byte_data))
text = codecs.decode(byte_data)
print("\nAfter the conversion: " ,text)
print("The type of the converted data is: ",type(text))

In the above code,

  • The codecs module is imported.
  • The byte_data variable contains the byte data. The variable and its type are printed. The type of the data will be bytes.
  • The decode() method from the codecs module is called on byte_data which will convert the bytes to string. The retrieved value will be stored in the variable named “text”.
  • The variable text is printed which will display the string ‘Coding is fun!’. Its type is also printed using the type() function which will be a string.

Output

The output displays that the bytes have been converted to a string value using the decodes() method from the codec module in Python:

Comparing decode() and str() for Byte Conversion

The decode() method uses any character encoding system while str() has lesser complexity and can change the string object to a byte object in a direct manner but uses particular standardized encoding systems. The key differences between the decode() and str() methods are:

functionCharacter EncodingSpeed Flexibility
str()Works with any character encoding systemComparatively fasterNot much flexible 
decode()Can only use the standardized encoding systems like UTF-8, ASCII, and UTF-16 fastOffers greater flexibility

Conclusion

Various Python methods like decode(), str(), codec module, and functions from the bytes class and Pandas library can convert the byte data to a string in Python. Converting the bytes to strings is helpful in many ways like making byte data more human readable during the sending and receiving of a file. In data science and NLP-based apps, conversion of bytes to string needs to be done as they are mostly involved with handling large text data. This article discusses different methods to convert Bytes into a string in Python and illustrates each method with an example.