use*_*744 1 python string range python-2.7
我要做的是打印10位数组合的所有可能组合...但是对于1我想要它打印0000000001,0000000002等.但是ZFill不工作.我究竟做错了什么?
a = range(0, 1000000)
print str(a).zfill(1000000)
Run Code Online (Sandbox Code Playgroud)
你正在做zfill的list对象.相反,您需要对zfill列表中的每个项目执行操作.以下是范围10的示例示例:
>>> a = range(0, 10)
# v this value represent the count of zeros
# v It should be `7` in your case
>>> [str(i).zfill(10) for i in a]
['0000000000', '0000000001', '0000000002', '0000000003', '0000000004', '0000000005', '0000000006', '0000000007', '0000000008', '0000000009']
Run Code Online (Sandbox Code Playgroud)
string.zfill(s,width)
在左侧填充数字字符串s,使用零数字,直到达到给定宽度.以符号开头的字符串处理正确.