In order to convert an integer to a binary, i have used this code :
>>> bin(6)
'0b110'
and when to erase the '0b', i use this :
>>> bin(6)[2:]
'110'
What can i do if i want to show 6
as 00000110
instead of 110
?
In order to convert an integer to a binary, i have used this code :
>>> bin(6)
'0b110'
and when to erase the '0b', i use this :
>>> bin(6)[2:]
'110'
What can i do if i want to show 6
as 00000110
instead of 110
?
.. or if you're not sure it should always be 8 digits, you can pass it as a parameter:
('0' * 7 + bin(6)[2:])[-8:]
or
right_side = bin(6)[2:] '0' * ( 8 - len( right_side )) + right_side