最好的方式填充零string

什么是填充一个数字string与零左边的最pythonic的方式,即,所以数字string具有特定的长度?

string:

>>> n = '4' >>> print n.zfill(3) 004 

而对于数字:

 >>> 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 

string格式文档 。

只需使用string对象的rjust方法即可。

这个例子将会产生一个10个字符的string,必要时填充。

 >>> t = 'test' >>> t.rjust(10, '0') >>> '000000test' 

对于数字:

 print "%05d" % number 

另请参阅: Python:string格式 。

编辑 :值得注意的是,截至2008年12月3日,这种格式化方法已被弃用, formatstring方法:

 print("{0:05d}".format(number)) # or print(format(number, "05d")) 

详情请参阅PEP 3101 。

 >>> '99'.zfill(5) '00099' >>> '99'.rjust(5,'0') '00099' 

如果你想要相反的话:

 >>> '99'.ljust(5,'0') '99000' 

适用于Python 2和Python 3:

 >>> "{:0>2}".format("1") # Works for both numbers and strings. '01' >>> "{:02}".format(1) # Only works for numbers. '01' 

str(n).zfill(width)将与string s, int s, float s一起工作,并且是Python 2. x和3. x兼容:

 >>> n = 3 >>> str(n).zfill(5) '00003' >>> n = '3' >>> str(n).zfill(5) '00003' >>> n = '3.0' >>> str(n).zfill(5) '003.0' 
 width = 10 x = 5 print "%0*d" % (width, x) > 0000000005 

看到所有激动人心的细节打印文档!

Python 3.x更新(7.5年后)

现在最后一行应该是:

 print("%0*d" % (width, x)) 

print()现在是一个函数,而不是一个声明。 请注意,我仍然更喜欢旧式的printf()风格,因为IMNSHO,它读得更好,因为这个,自1980年1月以来,我一直在使用这个符号。新的技巧。

对于来这里了解的人而不是简单的回答。 我特别为时间串做这些:

 hour = 4 minute = 3 "{:0>2}:{:0>2}".format(hour,minute) # prints 04:03 "{:0>3}:{:0>5}".format(hour,minute) # prints '004:00003' "{:0<3}:{:0<5}".format(hour,minute) # prints '400:30000' "{:$<3}:{:#<5}".format(hour,minute) # prints '4$$:3####' 

“0”符号用“2”填充字符replace什么,默认是空的

“>”符号将string左边的所有2“0”字符全部alignment

“:”符号format_spec

 >>> width = 4 >>> x = 5 >>> print("%0*d" %(width,x)) >>> 00005 

这将在python 3.x中工作

对于保存为整数的邮政编码:

 >>> a = 6340 >>> b = 90210 >>> print '%05d' % a 06340 >>> print '%05d' % b 90210 

快速时间比较:

 setup = ''' from random import randint def test_1(): num = randint(0,1000000) return str(num).zfill(7) def test_2(): num = randint(0,1000000) return format(num, '07') def test_3(): num = randint(0,1000000) return '{0:07d}'.format(num) def test_4(): num = randint(0,1000000) return format(num, '07d') def test_5(): num = randint(0,1000000) return '{:07d}'.format(num) def test_6(): num = randint(0,1000000) return '{x:07d}'.format(x=num) def test_7(): num = randint(0,1000000) return str(num).rjust(7, '0') ''' import timeit print timeit.Timer("test_1()", setup=setup).repeat(3, 900000) print timeit.Timer("test_2()", setup=setup).repeat(3, 900000) print timeit.Timer("test_3()", setup=setup).repeat(3, 900000) print timeit.Timer("test_4()", setup=setup).repeat(3, 900000) print timeit.Timer("test_5()", setup=setup).repeat(3, 900000) print timeit.Timer("test_6()", setup=setup).repeat(3, 900000) print timeit.Timer("test_7()", setup=setup).repeat(3, 900000) > [2.281613943830961, 2.2719342631547077, 2.261691106209631] > [2.311480238815406, 2.318420542148333, 2.3552384305184493] > [2.3824197456864304, 2.3457239951596485, 2.3353268829498646] > [2.312442972404032, 2.318053102249902, 2.3054072168069872] > [2.3482314132374853, 2.3403386400002475, 2.330108825844775] > [2.424549090688892, 2.4346475296851438, 2.429691196530058] > [2.3259756401716487, 2.333549212826732, 2.32049893822186] 

我做了不同重复的不同testing。 差异并不大,但是在所有testing中, zfill解决scheme都是最快的。

你也可以重复“0”,把它加到str(n)并得到最右边的宽度片。 快速和肮脏的小expression。

 def pad_left(n, width, pad="0"): return ((pad * width) + str(n))[-width:]