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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just to explain the parts of the formatting string:
{}places a variable into a string0takes the variable at argument position 0:adds formatting options for this variable (otherwise it would represent decimal6)08formats the number to eight digits zero-padded on the leftbconverts the number to its binary representationIf you’re using a version of Python 3.6 or above, you can also use f-strings: