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
?
|
Just to explain the parts of the formatting string:
|
|||||||||||||||||
|
Just another idea:
|
|||||
|
A bit twiddling method...
|
|||
|
eumiro's answer is better, however I'm just posting this for variety:
|
|||
|
.. or if you're not sure it should always be 8 digits, you can pass it as a parameter:
|
|||
|