← 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)
'0b10011010010'

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

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

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

>>> bin(1234)[2:]
'10011010010'

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

>>> int('10011010010', 2)
1234


integers binary


🐍 You might also find interesting: