Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Convert binary to string using Python
Next article icon

Convert binary to string using Python

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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) 

Output
geek 

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) 

Output
code 

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) 

Output
geek 

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) 

Output
geek 

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.


Next Article
Convert binary to string using Python
author
__Shubham_Singh__
Improve
Article Tags :
  • Python
  • python-string
  • Python string-programs
Practice Tags :
  • python

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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences