Ask coding questions

← Back to all posts
Binary
JustineHoresh

Hello folks. I need a python program that is converting a number in binary like 2=10.
Thanks in advance for your help and sorry for my broken english

Voters
JoanDe2
JustineHoresh
Comments
hotnewtop
finleyparr

num = int(input('Input number between 0 and 255 and I will convert it into binary.'))
if num < 256:
print(bin(num))

this code it quite short but it has an annoying 0b at the start and i dont know how to remove it, but as long as you can ignore it or can remove it yourself, feel free to use it, it took me a couple minutes to make

PabloJuan

@finleyparr
bin(int(x)).split('0b')[1]

olafh10

I created some code for you: https://repl.it/@olafh10/int-to-binary

This converts the input to binary, note that the program doesn't check if the input is a number. You can also just copy the intToBinary function for in your own program.

JustineHoresh

@olafh10 Thanks you very much

olafh10

@JustineHoresh No problem!