← Go Back

How to Convert a Number to Binary (And Vice Versa)

Using the bin() built-in function, which takes an integer and returns a string containing its binary representation:

>>> bin(1234)

To convert the binary number back to decimal use the int() built-in like this:

>>> int('0b10011010010', 2)

If you want to omit the 0b prefix when converting from decimal to binary, you can do:

>>> bin(1234)[2:]

The prefix is also not necessary to convert back to integer:

>>> int('10011010010', 2)

integers binary

🐍 You might also find interesting: