我想根据一个生成一个字符串int
和零一起。并且长度应该始终为5
不多也不少。
For example:
Consider a Integer: 1
Formatted String : 00001
Consider a Integer: 12
Formatted String : 00012
Consider a Integer: 110
Formatted String : 00110
Consider a Integer: 1111
Formatted String : 01111
Consider a Integer: 11111
Formatted String : 11111
Use the format()功能 https://docs.python.org/2/library/functions.html#format or the str.format() https://docs.python.org/2/library/stdtypes.html#str.format用零填充格式化整数的方法:
print format(integervalue, '05d')
print 'Formatted String : {0:05d}'.format(integervalue)
See the 格式规范迷你语言 https://docs.python.org/2/library/string.html#format-specification-mini-language;领先的0
格式中表示 0 填充,5
是最小字段宽度;任何比该数字短的数字都会被填充到全宽。
Demo:
>>> format(110, '05d')
'00110'
>>> 'Formatted String : {0:05d}'.format(12)
'Formatted String : 00012'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)