0 votes
1 view
in Python by (12.7k points)

I tried the below code to convert an integer to binary

>>> bin(6)  

'0b110'

I used the below code to erase the '0b'

>>> bin(6)[2:]

'110'

What I should do to show 6 as 00000110 instead of 110?

1 Answer

0 votes
by (26.4k points)

Try the following code:

>>> '{0:08b}'.format(6)

'00000110'

In case if you're using python version 3.6 and above, you can try:

>>> f'{6:08b}'

'00000110'

Want to learn python in detail? Join python course fast!!

Related questions

0 votes
1 answer
+3 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...