如何在数字前添加足够的零,以便使用Python为此数字设置六位数?

Rev*_*air 1 python

我在我的数据库中有一个巨大的数字列表(每个数字小于或等于6个数字),如:
90494
898333
898
13
等.

我想让它们格式化,使用python:
090494
898333
000898
000013
等...

非常感谢你的帮助

mgi*_*son 9

使用C风格格式修饰符的标准python插值应该有效:

print "%06d" % number
Run Code Online (Sandbox Code Playgroud)

或者,如果您更喜欢.formatPEP 3101中描述(很可能是python中字符串格式化的未来):

print '{0:06d}'.format(1)
Run Code Online (Sandbox Code Playgroud)

或者,如果您正在使用字符串,则可以使用zfill:

print '1'.zfill(6)
Run Code Online (Sandbox Code Playgroud)