Latest Code Tutorials

Python bin: How to Use bin() Function with Example

The bin() function is used to convert an integer number to a binary string. The result is a valid Python expression. The bin() is a built-in function that takes in integer x and returns the binary representation of x in a string format. If x is not an integer, then the _index()_ method needs to be implemented to obtain an integer as the return value instead of as a “TypeError” exception.

Python bin() Example

Python bin() is an inbuilt method that converts a decimal to binary data type. The bin() function returns a binary representation of an integer as the string. Python bin() reduces the time required to code and also removes the hassle.

See the Python bin() function syntax.


The function bin() takes the number as a parameter which is converted to the binary number. If the number is not an integer, then the __index__() method is implemented to return the integer.

See the following code example.

print("Binary of Eleven is:", bin(11))

See the following output.

➜  pyt python3
Binary of Eleven is: 0b1011
➜  pyt

As you can see in the example above, the bin() function returns the equivalent binary number prefixed with 0b of an integer.

If you don’t want that prefixed 0b, you can use the format() function.

print(format(11, 'b'))

See the following output.

➜  pyt python3
➜  pyt

#Convert an object to binary implementing __index__() method

See the following example in which we implement the object to the binary implementing __index__() method.

class Add:
    a = 11
    b = 19

    def __index__(self):
        return self.a + self.b

app = Add()
print('Equivalent binary number is:', bin(app))

See the following output.

➜  pyt python3
Equivalent binary number is: 0b11110
➜  pyt

That is how the __index__() function is used to return an integer when we don’t have an integer as an argument in the Python bin() function.

More Examples

See the following example.

a = 11
b = bin(a)

See the output.

➜  pyt python3
<class 'str'>
➜  pyt

The bin() method is available and compatible with both the Python version Python 2.x and 3.x.

Python bin() with float

See the following code example of the bin() with the float.

a = 11.19

See the following output.

➜  pyt python3
Traceback (most recent call last):
  File "", line 2, in <module>
TypeError: 'float' object cannot be interpreted as an integer
➜  pyt

So, Python bin() function can be used with the integers having different formats such as an octal, hexadecimal too. The function will take care of converting them into a binary string.

That’s all for the python bin() function to convert an integer to the binary string. We also learned that an Object could also be converted to the binary string representation by implementing the __index__() function that returns an integer.

Finally, Python bin Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.