Python自动给数字前面补0的方法

python中有一个zfill方法用来给字符串前面补0,非常有用

 
= "123"
= n.zfill(5)
assert == "00123"

zfill()也可以给负数补0

= "-123"
= n.zfill(5)
assert == "-0123"

对于纯数字,我们也可以通过格式化的方式来补0

= 123
= "%05d" % n
assert == "00123"

猜你喜欢

转载自www.cnblogs.com/rockniu2/p/9325778.html