Number to Binary Conversion in Python

If you have a decimal number, and want to get it’s binary value, you can use the built-in bin method.

decimal = 32
binary = bin(decimal)

# '0b100000'

We can see that it prepends the string with a 0b.

Let’s remove this to return a usable binary value:

decimal = 32
binary = str(bin(decimal)[2:])

# '100000'