In [1]:
s = '1234'
In [2]:
s_zero = s.zfill(8)
print(s_zero)
In [3]:
print(type(s_zero))
In [4]:
print(s.zfill(3))
In [5]:
s = '-1234'
print(s.zfill(8))
In [6]:
s = '+1234'
print(s.zfill(8))
In [7]:
s = 'abcd'
print(s.zfill(8))
In [8]:
n = 1234
In [9]:
print(type(n))
In [10]:
# print(n.zfill(8))
# AttributeError: 'int' object has no attribute 'zfill'
In [11]:
print(str(n).zfill(8))
In [12]:
s = '1234'
In [13]:
print(s.rjust(8, '0'))
In [14]:
print(s.ljust(8, '0'))
In [15]:
print(s.center(8, '0'))
In [16]:
s = '-1234'
In [17]:
print(s.rjust(8, '0'))
In [18]:
print(s.ljust(8, '0'))
In [19]:
print(s.center(8, '0'))
In [20]:
s = '1234'
In [21]:
print(format(s, '0>8'))
In [22]:
print('Zero Padding: {:0>8}'.format(s))
In [23]:
s = '-1234'
In [24]:
print(format(s, '0>8'))
In [25]:
print('Zero Padding: {:0>8}'.format(s))
In [26]:
print(format(int(s), '08'))
In [27]:
print('Zero Padding: {:0>8}'.format(int(s)))
In [28]:
n = 1234
In [29]:
print(format(n, '08'))
In [30]:
print('Zero Padding: {:08}'.format(n))
In [31]:
print(format(n, '08x'))
In [32]:
print('Zero Padding: {:08x}'.format(n))
In [33]:
n = -1234
In [34]:
print(format(n, '08'))
In [35]:
print('Zero Padding: {:08}'.format(n))
In [36]:
print(f'Zero Padding: {n:08}')
In [37]:
n = 1234
In [38]:
print('Zero Padding: %08d' % n)
In [39]:
n = -1234
In [40]:
print('Zero Padding: %08d' % n)
In [41]:
s = '1234'
In [42]:
print('Zero Padding: %08s' % s)
In [43]:
print('Zero Padding: %08d' % int(s))