Table of contents
1.
Introduction
2.
Understanding Bytes and Strings in Python  
2.1.
What are Bytes?  
2.2.
What are Strings?  
2.3.
Key Differences Between Bytes and Strings  
3.
Why Convert Bytes to Strings?  
4.
Using decode() method
4.1.
Syntax
4.2.
Example
5.
Using str() function
5.1.
Syntax
5.2.
Example
6.
Using codecs.decode() method
6.1.
Syntax
6.2.
Example
7.
Using map() without using the b prefix
7.1.
Example
8.
Using pandas to convert bytes to strings
8.1.
Example
9.
Encoding Errors  
9.1.
Common Encoding Errors  
9.2.
Example of Encoding Error  
9.3.
Handling Encoding Errors  
9.4.
Choosing the Right Encoding  
10.
Frequently Asked Questions
10.1.
What is the best way to convert bytes to a string in Python?
10.2.
Why does my byte-to-string conversion fail with UnicodeDecodeError?
10.3.
Can I convert bytes to a string without specifying encoding?
11.
Conclusion
Last Updated: Aug 19, 2025
Easy

How to Convert Bytes to String in Python

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, converting bytes to a string can be done using the .decode() method or the str() function. This conversion is essential when dealing with binary data, such as reading files, network communication, or handling encoded text. Choosing the correct encoding, like UTF-8, ensures proper conversion. 

How to Convert Bytes to String in Python

In this article, we will discuss different methods to convert bytes to a string in Python, along with examples demonstrating their implementation.

Understanding Bytes and Strings in Python  

Bytes and strings are two different ways to represent data in Python. Understanding the difference between them is crucial for working with files, networks, and APIs.  

What are Bytes?  

Bytes are a sequence of numbers ranging from 0 to 255. They represent raw binary data. For example, when you read a file in binary mode or receive data from a network, it’s often in the form of bytes. In Python, bytes are represented using the `bytes` or `bytearray` data types.  

This is an example of creating bytes in Python:  

Creating bytes using the bytes() function

data = bytes([72, 101, 108, 108, 111])   Represents "Hello" in ASCII
print(data)   
You can also try this code with Online Python Compiler
Run Code

 

Output: 

b'Hello'


In this example, the numbers 72, 101, 108, 108, and 111 correspond to the ASCII values of the characters 'H', 'e', 'l', 'l', and 'o'. The `b` prefix indicates that this is a bytes object.  

What are Strings?  

Strings are sequences of characters. They are human-readable and used to represent text. In Python, strings are enclosed in single or double quotes, like `"Hello"`.  

This is an example of a string:  

Creating a string

text = "Hello"
print(text)   
You can also try this code with Online Python Compiler
Run Code

 

Output: 

Hello

Key Differences Between Bytes and Strings  

1. Representation: Bytes are raw binary data, while strings are text.  
 

2. Encoding: Strings are encoded into bytes using a specific character encoding (like UTF-8).  
 

3. Mutability: Bytes are immutable, but `bytearray` is mutable. Strings are also immutable.  

Why Convert Bytes to Strings?  

When you receive data from external sources like files or networks, it’s often in bytes. To work with this data as text, you need to convert it into a string. This process is called decoding.  

For example: 

Converting bytes to string

byte_data = b'Hello'
string_data = byte_data.decode('utf-8')   Decoding using UTF-8 encoding
print(string_data)   
You can also try this code with Online Python Compiler
Run Code

 

Output: 

Hello


In this example, the `decode()` method is used to convert the bytes object `b'Hello'` into the string `"Hello"`. The `'utf-8'` argument specifies the encoding scheme.  

Using decode() method

The decode() method is the most common way to convert bytes to a string. It converts bytes into a string using a specific encoding, like UTF-8.

Syntax

bytes_variable.decode(encoding)

Example

byte_data = b'Hello, World!'
string_data = byte_data.decode("utf-8")
print(string_data)
You can also try this code with Online Python Compiler
Run Code


Output:

Hello, World!


Explanation:

  • The b before the string indicates that it is in bytes format.
     
  • decode("utf-8") converts the bytes into a string using UTF-8 encoding.
     
  • The output is a string that can be printed or used in further processing.

Using str() function

The str() function is another way to convert bytes to a string. However, it is less preferred because it includes additional formatting characters.

Syntax

str(bytes_variable, encoding)

Example

byte_data = b'Python Bytes'
string_data = str(byte_data, "utf-8")
print(string_data)
You can also try this code with Online Python Compiler
Run Code


Output:

Python Bytes


Explanation:

  • str(byte_data, "utf-8") converts the byte data to a string using UTF-8 encoding.
     
  • This method is useful but decode() is more commonly used as it provides better error handling.

Using codecs.decode() method

The codecs module in Python provides a method called decode(), which can be used for byte-to-string conversion.

Syntax

import codecs
codecs.decode(bytes_variable, encoding)

Example

import codecs
byte_data = b'Coding Ninjas'
string_data = codecs.decode(byte_data, "utf-8")
print(string_data)
You can also try this code with Online Python Compiler
Run Code

 

Output:

Coding Ninjas


Explanation:

  • The codecs module provides additional encoding and decoding options.
     
  • codecs.decode(byte_data, "utf-8") performs the conversion.
     
  • The output is a properly formatted string.

Using map() without using the b prefix

If byte data does not have the b prefix, we can use the map() function to convert each byte separately and join them into a string.

Example

byte_list = [80, 121, 116, 104, 111, 110]
string_data = "".join(map(chr, byte_list))
print(string_data)
You can also try this code with Online Python Compiler
Run Code


Output:

Python


Explanation:

  • Each number in byte_list represents an ASCII value.
     
  • map(chr, byte_list) converts each number to its corresponding character.
     
  • "".join(...) joins them into a single string.
     
  • The output is a string representation of the original byte values.

Using pandas to convert bytes to strings

The pandas library provides an easy way to convert bytes to strings, especially when working with large datasets.

Example

import pandas as pd
data = pd.Series([b'Hello', b'World', b'Python'])
string_data = data.str.decode("utf-8")
print(string_data)
You can also try this code with Online Python Compiler
Run Code


Output:

0    Hello
1    World
2    Python
dtype: object


Explanation:

  • pd.Series([...]) creates a pandas series with byte values.
     
  • .str.decode("utf-8") converts each byte value to a string.
     
  • The output is a series of strings that can be used for further processing.

Encoding Errors  

When converting bytes to strings, encoding errors can occur if the bytes are not compatible with the specified encoding. For example, if you try to decode bytes using the wrong encoding, Python will raise a `UnicodeDecodeError`. Understanding and handling these errors is essential for writing robust code.  

Common Encoding Errors  

1. UnicodeDecodeError: This occurs when the bytes cannot be decoded using the specified encoding.  
 

2. Incorrect Encoding: Using the wrong encoding (e.g., decoding UTF-8 bytes as ASCII) can lead to errors or incorrect results.  

Example of Encoding Error  

Let’s say you have bytes encoded in UTF-16, but you try to decode them using UTF-8:  

Bytes encoded in UTF-16

byte_data = b'\xff\xfeH\x00e\x00l\x00l\x00o\x00'   "Hello" in UTF-16


Attempting to decode using UTF-8 (incorrect encoding)

try:
    string_data = byte_data.decode('utf-8')
    print(string_data)
except UnicodeDecodeError as e:
    print(f"Error: {e}")


Output:  

Error: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte


In this example, the bytes are encoded in UTF-16, but we tried to decode them using UTF-8. This caused a `UnicodeDecodeError`.  

Handling Encoding Errors  

To handle encoding errors, you can use the `errors` parameter in the `decode()` method. This parameter allows you to specify how to handle errors during decoding. Common options are:  

1. `strict`: Raises a `UnicodeDecodeError` (default behavior).  
 

2. `ignore`: Ignores invalid bytes and continues decoding.  
 

3. `replace`: Replaces invalid bytes with a placeholder (e.g., `�`).  


Let’s discuss an example of handling errors:  

Bytes with invalid UTF-8 characters

byte_data = b'Hello\x80World'   \x80 is an invalid UTF-8 character


Using 'ignore' to skip invalid bytes

string_data_ignore = byte_data.decode('utf-8', errors='ignore')
print(string_data_ignore)   
You can also try this code with Online Python Compiler
Run Code

 

Output: 

HelloWorld


Using 'replace' to replace invalid bytes

string_data_replace = byte_data.decode('utf-8', errors='replace')
print(string_data_replace)   
You can also try this code with Online Python Compiler
Run Code


Output: 

Hello�World


In this example:  

  • The `ignore` option skips the invalid byte (`\x80`), resulting in `"HelloWorld"`.  
     
  • The `replace` option replaces the invalid byte with `�`, resulting in `"Hello�World"`.  

Choosing the Right Encoding  

To avoid encoding errors, always ensure you’re using the correct encoding. Common encodings are:  
 

  • UTF-8: Widely used and supports most characters.  
     
  • ASCII: Supports only basic English characters.  
     
  • UTF-16: Used for certain applications and supports a wide range of characters.  
     

Let’s take an example of decoding bytes using the correct encoding:  


Bytes encoded in UTF-16

byte_data = b'\xff\xfeH\x00e\x00l\x00l\x00o\x00'   "Hello" in UTF-16


Decoding using UTF-16 (correct encoding)

string_data = byte_data.decode('utf-16')
print(string_data)   
You can also try this code with Online Python Compiler
Run Code


Output: 

Hello

Frequently Asked Questions

What is the best way to convert bytes to a string in Python?

The best way is using the decode() method because it is simple, efficient, and provides better error handling.

Why does my byte-to-string conversion fail with UnicodeDecodeError?

This happens when the byte data is not encoded in the specified encoding format. Try using errors="ignore" or errors="replace" in decode().

Can I convert bytes to a string without specifying encoding?

No, specifying an encoding (like UTF-8) is necessary to correctly interpret the byte data.

Conclusion

In this article, we explored different methods to convert bytes to a string in Python. We discussed using the .decode() method, str() function, and codecs module for efficient conversion. Understanding these techniques helps in handling encoded data, reading binary files, and working with network communication in Python applications.

Live masterclass