yoshiserry yoshiserry - 23 days ago 13
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

You can use zfill method of a string

>>> print '12'.zfill(5)
00012
>>> print '9'.zfill(5)
00009
>>> print '90'.zfill(5)
00090
>>> print '10'.zfill(5)
00010
>>> print '1000'.zfill(5)
01000
>>> print '10001'.zfill(5)
10001
>>>

So following will generate such a list:

[str(num).zfill(5) for num in xrange(100000)]
Comments