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 02101I have executed the above example code and added the screenshot below.

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 SmithI have executed the above example code and added the screenshot below.

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, CAI have executed the above example code and added the screenshot below.

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:
- How to Check if a Given String is an Anagram of Another String?
- How to Swap Characters in a String Using Python?
- How to Convert a String to ASCII in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.