Zero Padding in Python
Last modified: 2023-08-29
We can easily pad the binary using Python.
Zero-padding to 8-bit
For zero-padding to 8-bit, specify 8
as the argument in the zfill
method.
'1101'.zfill(8)
# 00001101
'101011'.zfill(8)
# 00101011
Zero-padding to 16-bit
For zero-padding to 16-bit, specify 16
as the argument in the zfill
method.
'1101'.zfill(16)
# 0000000000001101
'11100110'.zfill(16)
# 0000000011100110
Zero-padding to 32-bit
For zero-padding to 32-bit, specify 32
as the argument in the zfill
method.
'1101'.zfill(32)
# 00000000000000000000000000001101
'11100110'.zfill(32)
# 00000000000000000000000011100110
Zero-padding to 64-bit
For zero-padding to 64-bit, specify 64
as the argument in the zfill
method.
'1101'.zfill(64)
# 0000000000000000000000000000000000000000000000000000000000001101
'11100110'.zfill(64)
# 0000000000000000000000000000000000000000000000000000000011100110