Python
binary conversion
string manipulation
Python3
programming tutorial

How to convert 'binary string' to normal string in Python3?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Python 3, converting a binary string into readable text means deciding what the binary digits represent. If the string contains bytes written as groups of eight bits, the usual solution is to convert each group into an integer, build a bytes object, and then decode it with the correct text encoding.

Start With the Data Format

The phrase "binary string" can mean different things. It might be:

  • a text value like "01001000 01101001"
  • a Python bytes object such as b"Hi"
  • one long string of bits with no separators

The conversion approach depends on which one you have. For readable text encoded as bytes, the most common case is eight bits per byte.

Converting Space-Separated Bits to Text

Here is a clean Python 3 example for input like "01001000 01101001":

python
1def binary_to_text(binary_string: str, encoding: str = "utf-8") -> str:
2    parts = binary_string.split()
3    byte_values = [int(part, 2) for part in parts]
4    return bytes(byte_values).decode(encoding)
5
6print(binary_to_text("01001000 01101001"))
7# Hi

Each part is converted from base 2 into an integer. bytes(byte_values) builds the raw byte sequence, and decode() turns those bytes into a normal Python string.

Converting a Continuous Bit String

If the bits arrive as one continuous string such as "0100100001101001", split it into groups of eight first:

python
1def chunk_bits(value: str, size: int = 8) -> list[str]:
2    if len(value) % size != 0:
3        raise ValueError("Bit string length must be a multiple of 8")
4    return [value[i:i + size] for i in range(0, len(value), size)]
5
6
7def binary_to_text_no_spaces(binary_string: str, encoding: str = "utf-8") -> str:
8    groups = chunk_bits(binary_string, 8)
9    byte_values = [int(group, 2) for group in groups]
10    return bytes(byte_values).decode(encoding)
11
12print(binary_to_text_no_spaces("0100100001101001"))
13# Hi

The length check matters. If the bit count is not divisible by eight, you do not have a complete sequence of bytes.

Encoding Still Matters

Converting bits to bytes is only half of the job. You also need the correct character encoding. ASCII works for plain English text, but UTF-8 is the safer default for general Python 3 code.

For example, this binary data represents the UTF-8 encoding of the euro sign:

python
bits = "11100010 10000010 10101100"
print(binary_to_text(bits, encoding="utf-8"))
# €

If you decode those bytes with the wrong encoding, Python may raise an exception or produce the wrong character.

When the Input Is Already bytes

Sometimes the value is called a binary string, but it is already a Python bytes object. In that case, do not convert bit by bit. Just decode it directly:

python
payload = b"Hello"
text = payload.decode("utf-8")
print(text)

This is both simpler and faster than turning the bytes into a textual bit representation and then converting back.

Common Pitfalls

The first mistake is confusing a string of characters containing 0 and 1 with a bytes object. They are not the same type and should be handled differently.

Another issue is forgetting to group the bits into bytes. Text decoding works on byte values, not on one giant base-2 integer.

Developers also assume ASCII when the data is actually UTF-8 or another encoding. If the characters look corrupted, the encoding is one of the first things to check.

Summary

  • Convert binary text to bytes by grouping bits into eight-bit chunks and parsing each chunk with base 2.
  • Decode the resulting bytes with the correct character encoding, usually UTF-8.
  • Space-separated bit strings and continuous bit strings need slightly different preprocessing.
  • If the input is already a bytes object, decode it directly.
  • Validate the input length so incomplete byte groups fail early instead of producing confusing output.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.