Python Convert To Binary And Keep Leading Zeros

[Solved] Python Convert To Binary And Keep Leading Zeros | Perl - Code Explorer | yomemimo.com
Question : python print binary leading zeros

Answered by : classy-answer

>>> print(bin(160)) # This version gives the 0b prefix for binary numbers.
0b10100000
>>> print(format(160,'08b')) # This specifies leading 0, 8 digits, binary.
10100000
>>> print('{:08b}'.format(160)) # Another way to format.
10100000
>>> print(f'{160:08b}') # Python 3.6+ new f-string format.
10100000

Source : https://stackoverflow.com/questions/47664866/converting-a-calculated-decimal-number-into-8-bit-binary | Last Update : Thu, 10 Mar 22

Answers related to python convert to binary and keep leading zeros

Code Explorer Popular Question For Perl