python print binary leading zeros

68

python print binary leading zeros -

>>> 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

Comments

Submit
0 Comments