How to Convert Bytes to String in Python ?
Last Updated :
14 Apr, 2025
We are given data in bytes format and our task is to convert it into a readable string. This is common when dealing with files, network responses, or binary data. For example, if the input is b'hello', the output will be 'hello'.
This article covers different ways to convert bytes into strings in Python such as:
- Using decode() method
- Using str() function
- Using codecs.decode() method
- Using map() without using the b prefix
- Using pandas to convert bytes to strings
Let's explore them one by one:
Using .decode() method
This method is used to convert from one encoding scheme, in which the argument string is encoded to the desired encoding scheme. This works opposite to the encode.
Python
s = b'GeeksForGeeks'
res = s.decode()
print(res, type(res))
OutputGeeksForGeeks <class 'str'>
Explanation:
- decode() converts byte sequences into human-readable strings.
- It assumes the byte object is UTF-8 encoded unless specified otherwise.
Using str() function
The str() function of Python returns the string version of the object.
Python
s = b'GeeksForGeeks'
print(type(s))
res = str(s, 'utf-8')
print(res, type(res))
Output<class 'bytes'>
GeeksForGeeks <class 'str'>
Explanation:
- str() constructor works similarly to decode() when passed with an encoding.
- It’s a useful alternative, especially when you're not calling it on a byte object directly.
Using codecs.decode() method
The codecs module provides an alternative way to decode bytes, especially useful when dealing with different encodings.
Python
import codecs
s = b'GeeksForGeeks'
res = codecs.decode(s)
print(res, type(res))
OutputGeeksForGeeks <class 'str'>
Using map() without using the b prefix
If you have a list of ASCII values, you can manually convert them into characters and join them into a string.
Python
ascII = [103, 104, 105]
string = ''.join(map(chr, ascII))
print(string)
Explanation:
- Each ASCII value is converted into its character using chr().
- Then characters are then joined into a complete string using .join() method.
Using pandas to convert bytes to strings
In this example, we are importing a pandas library, and we will take the input dataset and apply the decode() function.
Python
import pandas as pd
dic = {'column' : [ b'Book', b'Pen', b'Laptop', b'CPU']}
data = pd.DataFrame(data=dic)
x = data['column'].str.decode("utf-8")
print(x)
Output0 Book
1 Pen
2 Laptop
3 CPU
Name: column, dtype: object
Explanation:
- str.decode() is applied element-wise to each entry in the column.
- Especially useful when handling data from binary files or APIs in pandas.
Similar Reads
Convert Hex String to Bytes in Python Converting a hexadecimal string to bytes in Python involves interpreting each pair of hexadecimal characters as a byte. For example, the hex string 0xABCD would be represented as two bytes: 0xAB and 0xCD. Letâs explore a few techniques to convert a hex string to bytes.Using bytes.fromhex() bytes.fro
2 min read
How to Convert Int to Bytes in Python? The task of converting an integer to bytes in Python involves representing a numerical value in its binary form for storage, transmission, or processing. For example, the integer 5 can be converted into bytes, resulting in a binary representation like b'\x00\x05' or b'\x05', depending on the chosen
2 min read
How to Convert Bytes to Int in Python? Converting bytes to integers in Python involves interpreting a sequence of byte data as a numerical value. For example, if you have the byte sequence b'\x00\x01', it can be converted to the integer 1.Using int.from_bytes()int.from_bytes() method is used to convert a byte object into an integer. It a
3 min read
Convert Hex to String in Python Hexadecimal (base-16) is a compact way of representing binary data using digits 0-9 and letters A-F. It's commonly used in encoding, networking, cryptography and low-level programming. In Python, converting hex to string is straightforward and useful for processing encoded data.Using List Comprehens
2 min read
Convert a String to Utf-8 in Python Unicode Transformation Format 8 (UTF-8) is a widely used character encoding that represents each character in a string using variable-length byte sequences. In Python, converting a string to UTF-8 is a common task, and there are several simple methods to achieve this. In this article, we will explor
3 min read