Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Python bin() Method

Python bin() method converts a given integer to it’s equivalent binary string. If the method parameter is some other object (not integer) then it has to __index__() method (with double underscores on the both sides) which must return an integer value for the object. The prefix 0b represents …

Lokesh Gupta

October 1, 2022

Python Built-in Functions
Python Basics
Python

Python bin() method converts a given integer to it’s equivalent binary string. If the method parameter is some other object (not integer) then it has to __index__() method (with double underscores on the both sides) which must return an integer value for the object.

The prefix 0b represents that a given string is a binary string.

1. Syntax

The syntax of bin() method is:

binaryString = bin( intValue );

Method parameters

This method takes single required parameter intValue of int datatype whose binary equivalent is to be calculated.

If parameter is not an int type, parameter object should implement __index__() method which should return an integer.

Method return Value

The return value is the binary string equivalent to the given integer.

TypeError

If the method parameter is not an integer, bin() raises a TypeError exception with message that the type cannot be interpreted as an integer.

2. Converting an integer to binary string

Python program to convert an integer to binary string using the bin() method.

intValue = 10
print('The binary string of 10 is:', bin(intValue))

intValue = -10
print('The binary string of -10 is:', bin(intValue))

names = ['Lokesh', "Alex"]
print('The binary equivalent of names list is:', bin(names))

Program output.

The binary string of 10 is:   0b1010
The binary string of -10 is: -0b1010
TypeError: 'list' object cannot be interpreted as an integer

3. Converting an object implementing __index__() method

Python program to convert an abject to binary string.The object implements __index__() method.

class Employee:
    id = 10
    age = 28
    name = 'Lokesh'
    
    def __index__(self):
        return self.id
        
print('The binary equivalent is:', bin(Employee()))

Program output.

The binary equivalent of quantity is: 0b1010

4. Removing “0b” prefix

To remove the prefixed “0b” string, use array slicing on the string.

intValue = 10
print('The binary string of 10 is:', bin(intValue)[2:])

Program output.

The binary string of 10 is:  1010

Happy Learning !!

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python Examples

Python Built-in Functions

  • Python abs()
  • Python all()
  • Python any()
  • Python ascii()
  • Python bin()
  • Python input()
  • Python range()
  • Python print()

Python String Functions

  • String capitalize()
  • String count()
  • String endswith()
  • String split()
  • String startswith()

Table of Contents

  • 1. Syntax
      • Method parameters
      • Method return Value
      • TypeError
  • 2. Converting an integer to binary string
  • 3. Converting an object implementing __index__() method
  • 4. Removing “0b” prefix
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Python ascii() Method

Next

Python Dictionary

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap