how to loop from 0000 to 9999 and convert the number to the relative string?

One way to get what you want is to use string formatting:

>>> for i in range(10):
...     '{0:04}'.format(i)
... 
'0000'
'0001'
'0002'
'0003'
'0004'
'0005'
'0006'
'0007'
'0008'
'0009'

So to do what you want, do this:

print("\n".join(['{0:04}'.format(num) for num in range(0, 10000)]))

try this

print"\n".join(["%#04d" % num for num in range(0, 9999)])

Simply using str.rjust:

print "\n".join([str(num).rjust(4, '0') for num in range(0, 1000)])

Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).