yoshiserry yoshiserry - 1 year ago 78
Python Question

generate all possibe 5 digit numbers from format 00000 to 99999 using python

Using the range function or another method in python I would like to calculate all the numbers from 00000 to 99999 where the possible values for a digit in any position could be 0-9 and where the resultant number includes all the leading 0's and must be 5 digits.

So the first number would be 000000 the second 00001 third 00002 to 00009 then 00010 etc.

Answer Source

You can use zfill method of a string

>>> print '12'.zfill(5)
>>> print '9'.zfill(5)
>>> print '90'.zfill(5)
>>> print '10'.zfill(5)
>>> print '1000'.zfill(5)
>>> print '10001'.zfill(5)

So following will generate such a list:

[str(num).zfill(5) for num in xrange(100000)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download