Convert Bytes to String in Python

Convert Bytes to String in Python

You can convert bytes to strings in Python using the decode() method.

Converting bytes to strings is essential for processing the text data in Python. Python provides multiple methods to convert the bytes to strings. In this blog let’s explore the different methods with examples for each.

Table of Contents:

Methods to convert bytes to string in Python 3

Method 1: Using decode() to convert bytes to string in Python

The decode() method is the most commonly used method in Python for converting bytes to strings.

Example:

# Define a bytes object 
byte_data = b"\xc3\xa7af\xc3\xa9"

# Convert bytes to string using decode() 
string_data = byte_data.decode(encoding="utf-8")

# Print the decoded string
print(string_data)

Output:

Explanation:

The decode() converts the bytes into a string. This method is suitable for the basic conversion of bytes to strings.

Method 2: Using str() to convert bytes to string in Python

The str() function provides an effective way to convert bytes to strings. The prefix b has to be specified while defining the byte data.

Example:

# Define a byte object
byte_data = b"Python \xf0\x9f\x90\x8d"

# Convert byte to string using str()
string_data = str(byte_data, 'utf-8')

# Print the converted string data
print(string_data)

Output:

Explanation:

The str() directly converts bytes to a string. This method is useful for quick conversion from bytes to strings. 

Method 3: Using map() to convert bytes to string in Python

The map() function converts bytes to strings by applying the chr() function to each byte of data.

Example:

# Define the byte object
byte_data= [72, 101, 108, 108, 111]

# Convert byte data to string data using map()  
string_data = “ ”.join(map(chr, byte_data))  

# Print the converted string data
print(string_data)

Output:

Explanation:

The map(chr, byte_data) applies chr() to each byte, which converts it to a character. The join() combines the characters into a single string. This method works perfectly for ASCII data.

Method 4: Using Pandas to convert bytes to string in Python

The pandas method is useful in converting bytes to strings, especially when you are working with data frames in Python.

Example:

# Import the pandas module

import pandas as pd

# Create a dataframe with byte data

df = pd.dataframe({'Bytes': [b"\xc3\xa7af\xc3\xa9”]}) 

# Convert bytes to string using .str.decode()

dff['String'] = df['Bytes'].str.decode('utf-8')

# Print the dataframe

print(df)

Output:

Explanation:

The df[‘Bytes’] contains byte data in a pandas DataFrame. The output will correctly display the converted string çafé. This method is useful when handling large datasets.

Method 5: Using the codecs.decode() to convert bytes to string in Python

Using the codecs.decode() function helps in converting bytes to strings in Python by specifying the encoded format. This method is useful in handling different encoded data.

Example:

import codecs 

# Define a byte object 

byte_data =b"\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82"

# Convert byte data to string data using codecs.decode() 

string_data = codecs.decode(byte_data, "utf-8") 

# Print the converted string data

print(string_data)

Output:

Explanation:

The codecs.decode() decodes the byte object into a string using UTF-8 encoding. The output is привет which refers to hello in Russian. This method is useful for encoded conversions of the byte data.

Conclusion

Each of the above methods offers an effective way to convert bytes to strings in Python. The decode() method is the most reliable method, while pandas are useful for handling large datasets. Understanding these approaches allows you to process the text data effectively.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner