我想要做的是打印 10 位数字组合的所有可能组合...但是对于 1,我希望它打印 0000000001、0000000002 等。但是 ZFill 不起作用。我做错了什么?
a = range(0, 1000000)
print str(a).zfill(1000000)
请您参考如下方法:
您正在对 list 对象执行 zfill。相反,您需要对列表的每个项目执行 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']
根据 str.zfill() document :
string.zfill(s, width)
Pad a numeric string s on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly.
