Convert binary to string using Python
Last Updated :
12 Apr, 2025
We are given a binary string and need to convert it into a readable text string. The goal is to interpret the binary data, where each group of 8 bits represents a character and decode it into its corresponding text. For example, the binary string '01100111011001010110010101101011' converts to 'geek'. Let's explore different methods to perform this conversion efficiently.
Using list comprehension
This method breaks the binary string into chunks of 8 bits and converts each chunk into a character using int() and chr() functions. It’s a clean and readable way to convert binary to text and works well for small to medium-length strings.
Python
b = '01100111011001010110010101101011'
s = ''.join(chr(int(b[i:i+8], 2)) for i in range(0, len(b), 8))
print(s)
Explanation: binary string b is split into 8-bit chunks using a for loop inside a list comprehension. Each chunk is converted from binary to decimal using int(..., 2), then to a character using chr(...). Finally, join() combines all characters into a single string.
Using int().to_bytes().decode()
In this approach, the whole binary string is first turned into an integer. Then it’s converted to bytes and finally decoded into a string. It’s a fast and compact method, especially useful when dealing with longer binary inputs.
Python
b = '01100011011011110110010001100101'
s = int(b, 2).to_bytes(len(b) // 8, 'big').decode()
print(s)
Explanation: int(b, 2) converts the binary string into an integer. .to_bytes(len(b) // 8, 'big') turns it into a byte sequence. .decode() then converts the bytes into a readable string.
Using codecs.decode()
This method first converts the binary into a hexadecimal string, then decodes it using the codecs module. It’s handy when working with encoded binary data and gives a bit more control during the conversion process.
Python
import codecs
b = '01100111011001010110010101101011'
hex_string = hex(int(b, 2))[2:]
if len(hex_string) % 2 != 0:
hex_string = '0' + hex_string
s = codecs.decode(hex_string, 'hex').decode()
print(s)
Explanation: int(b, 2) converts the binary string to an integer. hex(...)[2:] gets its hex representation without the 0x prefix. If the hex string has an odd length, a '0' is prepended to ensure proper byte alignment. codecs.decode(..., 'hex') converts the hex to bytes and .decode() converts it to a regular string.
Using for loop
If you prefer a more step-by-step approach, using a simple for loop can help. It processes the binary string 8 bits at a time, converts each part into a character and builds the final string manually.
Python
b = '01100111011001010110010101101011'
s = ''
for i in range(0, len(b), 8):
s += chr(int(b[i:i+8], 2))
print(s)
Explanation: for loop takes each 8-bit chunk (b[i:i+8]), converts it from binary to decimal using int(..., 2), then changes it to a character using chr(...). Each character is added to the string s.
Similar Reads
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
How to Convert Bytes to String in Python ? 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 Pyt
2 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
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
C strings conversion to Python For C strings represented as a pair char *, int, it is to decide whether or not - the string presented as a raw byte string or as a Unicode string. Byte objects can be built using Py_BuildValue() as C // Pointer to C string data char *s; // Length of data int len; // Make a bytes object PyObject *ob
2 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 Set to String in Python Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}"
2 min read
Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr
1 min read
Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver
3 min read
Convert Unicode String to a Byte String in Python Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular
2 min read