今天爱分享给大家带来python如何填充0到数字字符串中保证统一长度【面试题详解】,希望能够帮助到大家。
对于字符串
>>> n = '4' >>> print n.zfill(3) >>> '004'
对于数字
>>> n = 4
>>> print '%03d' % n
>>> 004
>>> print "{0:03d}".format(4) # python >= 2.6
>>> 004
>>> print("{0:03d}".format(4)) # python 3
>>> 004
原文链接:https://itblood.website/3921.html,转载请注明出处。

![迷宮都市アサイラム[Ver1.09][PC/3.30G] [ARPG/更新]](/wp-content/uploads/replace/2025/07/19/f46e2b5dd833e0e7acf0dac6dde7fa3e.webp)