Number combinations

Tim Chase python.list at tim.thechases.com
Wed Jul 19 10:18:58 EDT 2006


> Just wondering if there is a better way of generating a 4 digit number
> (that gets converted to a string), ive got the following code which
> generates strings between 0000-9999.
> 
> <code>
> 
> for a in range(0,10):
>     for b in range(0,10):
>         for c in range(0,10):
>             for d in range(0,10):
>                 print "%s%s%s%s" %(str(a), str(b), str(c),str(d)
> 
> </code>

Is there something wrong with

	for i in xrange(0,10000): print "%04i" % i

If you need the individual digits for something, you can use

	for i in xrange(0,10000):
		d1,d2,d3,d4 = list("%04i" % i)
		# do something with d1,d2,d3,d4

somewhat indelicate, but it works for me. :)

-tkc







More information about the Python-list mailing list