Convert decimal to binary in python

Paul picture Paul · Aug 20, 2010 · Viewed 473.7k times · Source

Is there any module or function in python I can use to convert a decimal number to its binary equivalent? I am able to convert binary to decimal using int('[binary_value]',2), so any way to do the reverse without writing the code to do it myself?

Answer

aaronasterling picture aaronasterling · Aug 20, 2010

all numbers are stored in binary. if you want a textual representation of a given number in binary, use bin(i)

>>> bin(10)
'0b1010'
>>> 0b1010
10