How to Convert Bytes to Strings in Python?

In this tutorial, I will explain how to convert bytes to strings in Python. As a data scientist working on various projects for clients across the USA, I recently encountered this issue when processing some byte-encoded data from our servers. I researched more about this topic and I will explain several methods to accomplish this, with clear examples.

Convert Bytes to Strings in Python

Before getting into the conversion methods, let’s clarify the difference between bytes and strings in Python:

  • Strings are sequences of Unicode characters, like “Hello, John!” or “Main St, New York, NY 10001”. They are used to represent text.
  • Bytes are sequences of 8-bit integers, often used to represent binary data, like b’\x48\x65\x6C\x6C\x6F’ or b’Main St, New York, NY 10001′. They are used when storing or transmitting data.

Read How to Check if a String is a GUID in Python?

Method 1: Use the decode() Method

The most simple way to convert bytes to strings in Python is by using the decode() method. This method decodes the byte sequence into a string using the specified encoding (default is ‘utf-8’). Here’s an example:

address_bytes = b'123 Main St, Boston, MA 02101' 
address_str = address_bytes.decode('utf-8')
print(address_str) 

Output:

123 Main St, Boston, MA 02101

I have executed the above example code and added the screenshot below.

Convert Bytes to Strings in Python

Check out How to Check if a String is Comma Separated in Python?

Method 2: Use the str() Constructor

Another way to convert bytes to string is by using the str() constructor. This works similarly to the decode() method:

name_bytes = b'John Smith'
name_str = str(name_bytes, 'utf-8') 
print(name_str)  

Output:

John Smith

I have executed the above example code and added the screenshot below.

How to Convert Bytes to Strings in Python

Read How to Check if String Length is Greater Than 0 in Python?

Method 3: Use the Codecs Module

Python’s built-in codecs module provides another way to convert bytes to string. This can be useful for more advanced encoding scenarios:

import codecs

city_bytes = b'San Francisco, CA'
city_str = codecs.decode(city_bytes, 'utf-8')
print(city_str)  

Output:

San Francisco, CA

I have executed the above example code and added the screenshot below.

Convert Bytes to Strings in Python codecs module

Check out How to Check if a String Contains Any Special Character in Python?

Handle Encoding Errors

Sometimes, the byte sequence may contain invalid characters for the specified encoding. In such cases, you can pass an optional error parameter to handle these errors:

corrupted_bytes = b'123 Main St, Boston, MA \x8f\x9a\x5c'
address_str = corrupted_bytes.decode('utf-8', errors='replace')
print(address_str)  

Output:

123 Main St, Boston, MA ???\

The errors parameter can take values like ‘strict’ (default), ‘ignore’, ‘replace’, etc., to specify how to handle encoding errors.

Read How to Check if a String is an Emoji in Python?

Conclusion

In this tutorial, I helped you to learn how to convert bytes to strings. I covered mainly three methods to achieve this task as decode() method, the str() constructor, and the codecs module. I also discussed how to handle encoding errors.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.