Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k 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

+3 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...