
python 字符串
zfill
在字符串前填充0到指定长度
>>> n = '4'
>>> print(n.zfill(3))
004
其他类似方法
-
format
>>> n = 4 >>> print('%03d' % n) 004 >>> print(format(n, '03')) # python >= 2.6 004 >>> print('{0:03d}'.format(n)) # python >= 2.6 004 >>> print('{foo:03d}'.format(foo=n)) # python >= 2.6 004 >>> print('{:03d}'.format(n)) # python >= 2.7 + python3 004 >>> print('{0:03d}'.format(n)) # python 3 004 >>> print(f'{n:03}') # python >= 3.6 004
2.rjust/ljust 左对齐 右对齐
>>> '99'.rjust(5,'0')
'00099'
知识点
千分割符,适合python3.x
‘{:,}’.format(1234567890)
‘1,234,567,890’




近期评论