Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
Python - Integers String to Integer List
Next article icon

Integer to Binary String in Python

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We have an Integer and we need to convert the integer to binary string and print as a result. In this article, we will see how we can convert the integer into binary string using some generally used methods.

Example:

Input : 77
Output : 0b1001101
Explanation: Here, we have integer 77 which we converted into binary string

Integer to Binary String in Python

Below are some of the ways to convert Integer to Binary String in Python:

  • Using bin() Function,
  • Using format() Function,
  • Using bitwise Operator

Integer to Binary String Using bin() function

In this example, The variable `ans` holds the binary string representation of the integer `num` using the `bin()` function, and the result is printed, displaying "The binary string is" followed by the binary string.

Python3

num = 77  ans = bin(num)  # The binary string 'ans' is printed print(type(num)) print("The binary string is", ans) print(type(ans)) 


Output
<class 'int'> The binary string is 0b1001101 <class 'str'> 

Integer to Binary String Using format() Function

In this example, The variable `ans` stores the binary string representation of the integer `num` using the `format()` method with the 'b' format specifier, and it is then printed with the message "The binary string is".

Python3

num = 81  ans = format(num, 'b') print(type(num)) print("The binary string is", ans) print(type(ans)) 


Output
<class 'int'> The binary string is 1010001 <class 'str'> 

Integer to Binary String Using bitwise Operator

In this example, below code , a binary string is manually constructed by iteratively taking the remainder of the number divided by 2 and appending it to the left of the `binary_string`. The process continues until the number becomes zero. The resulting binary representation is then printed using an f-string.

Python3

# Manual conversion with bitwise operations binary_string = '' number = 42  while number > 0:     binary_string = str(number % 2) + binary_string     number //= 2  # Handle the case when num is 0 binary_result = binary_string if binary_string else '0'  # Example usage print(type(number)) print(f"The binary representation of {number} is: {binary_result}") print(type(binary_result)) 


Output
<class 'int'> The binary representation of 0 is: 101010 <class 'str'> 

Next Article
Python - Integers String to Integer List
author
reshmah
Improve
Article Tags :
  • Python
  • Python Programs
  • python-string
  • python-basics
Practice Tags :
  • python

Similar Reads

  • Python - Integers String to Integer List
    In this article, we will check How we can Convert an Integer String to an Integer List Using split() and map()Using split() and map() will allow us to split a string into individual elements and then apply a function to each element. [GFGTABS] Python s = "1 2 3 4 5" # Convert the string to
    2 min read
  • Python - Binary list to integer
    A binary list represents binary digits (0s and 1s) as individual elements of a list. This article will explore various methods to convert a binary list into an integer. Using int() with String ConversionThis is the most efficient method. By joining the binary list into a string and using the built-i
    3 min read
  • Python - Convert Binary tuple to Integer
    Given Binary Tuple representing binary representation of a number, convert to integer. Input : test_tup = (1, 1, 0) Output : 6 Explanation : 4 + 2 = 6. Input : test_tup = (1, 1, 1) Output : 7 Explanation : 4 + 2 + 1 = 7. Method #1 : Using join() + list comprehension + int() In this, we concatenate t
    5 min read
  • Insert a number in string - Python
    We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num
    2 min read
  • Python Program to Generate Random binary string
    Given a number n, the task is to generate a random binary string of length n.Examples: Input: 7 Output: Desired length of random binary string is: 1000001 Input: 5 Output: Desired length of random binary string is: 01001 Approach Initialize an empty string, say key Generate a randomly either "0" or
    2 min read
  • Convert String to bytes-Python
    The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Let’s explore different method
    2 min read
  • Python - Convert Alternate String Character to Integer
    Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the alternate list of string to integers is quite common in development domain. Let’s discuss few ways to solve this particular problem. Method #1 : Naive Method This is most generic met
    5 min read
  • Convert Floating to Binary - Python
    The task of converting a floating-point number to its binary representation in Python involves representing the number in the IEEE 754 format, which consists of a sign bit, an exponent and a mantissa. For example, given the floating-point number 10.75, its IEEE 754 32-bit binary representation is "0
    3 min read
  • Binary Search (bisect) in Python
    Binary Search is a technique used to search element in a sorted list. In this article, we will looking at library functions to do Binary Search.Finding first occurrence of an element. bisect.bisect_left(a, x, lo=0, hi=len(a)) : Returns leftmost insertion point of x in a sorted list. Last two paramet
    2 min read
  • Convert Bytearray to Hexadecimal String - Python
    We are given a byte array a=bytearray([15, 255, 100, 56]) we need to convert it into a hexadecimal string that looks like: 0fff6438. Python provides multiple ways to do this: Using the .hex() method (built-in)Using binascii.hexlify()Using format() with join()Let’s explore each method with examples.
    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