Python Convert Bytes to String

In this tutorial, we will take a look at how to convert bytes to string in Python. 

We can convert bytes to string using the below methods 

  1. Using decode() method
  2. Using str() method
  3. Using codecs.decode() method

Method 1: Using decode() method

The bytes class has a decode() method. It takes the byte object and converts it to string. It uses the UTF-8 encoding by default if you don’t specify anything. The decode() method is nothing but the opposite of the encode.

# Python converting bytes to string using decode()

data = b'ItsMyCode \xf0\x9f\x8d\x95!'
print(data)
print("Before conversion type is", type(data))

# coversion happens from bytes to string
output = data.decode()
print(output)
print("Coverted type is ", type(output))

Output

Before conversion type is <class 'bytes'>
ItsMyCode 🍕!
Coverted type is  <class 'str'>

Method 2: Using str() function

Another easiest way to convert from Bytes to string is using the str() method. You need to pass the correct encoding to this method else it will lead to incorrect conversion.

# Python converting bytes to string using str()

data = b'ItsMyCode \xf0\x9f\x8d\x95!'
print(data)
print("Before conversion type is", type(data))

# coversion happens from bytes to string
output = str(data,'UTF-8')
print(output)
print("Coverted type is ", type(output))

Output

Before conversion type is <class 'bytes'>
ItsMyCode 🍕!
Coverted type is  <class 'str'>

Method 3: Using codecs.decode() method

codecs module comes as a standard built-in module in Python, and it has a decode() method which takes the input bytes and returns the string as output data.

# Python converting bytes to string using decode()
import codecs

data = b'ItsMyCode \xf0\x9f\x8d\x95!'
print(data)
print("Before conversion type is", type(data))

# coversion happens from bytes to string
output = codecs.decode(data)
print(output)
print("Coverted type is ", type(output))

Output

Before conversion type is <class 'bytes'>
ItsMyCode 🍕!
Coverted type is  <class 'str'>
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
How To Rename Columns In Pandas Dataframe

How to rename columns in Pandas DataFrame

Table of Contents Hide Method 1: Rename Specific column names in Pandas DataFrameMethod 2: Rename all column names in Pandas DataFrameMethod 3: Replace specific characters in Columns of Pandas DataFrame…
View Post
Sort Dictionary By Value In Python

Sort Dictionary by value in Python

Table of Contents Hide Python sorted() function to rescueSyntax: sorted(iterable, key, reverse)Sort a Dictionary by ValueExample 1: Basic Sorting in PythonExample 2 : Sorting different Data Types in PythonExample 3: Sort…
View Post
Python Ascii()

Python ascii()

Table of Contents Hide ascii() Syntax ascii() Parametersascii() Return ValueExample 1: How ascii() method works?Example 2: ascii() vs print() The ascii() in Python is a built-in function that returns a printable…
View Post
Python Callable()

Python callable()

Table of Contents Hide callable() Syntax callable() Parameterscallable() Return ValueExample 1: How does callable() works?Example 2: When an Object is callable Example 3: When an Object is NOT callable  The callable() function in…
View Post