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'
Code language: Python (python)

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'
Code language: Python (python)

Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments