I want to create a list of values that each start with TI and range from 00000-50000. Any idea of how to do this in Python?
Asked
Active
Viewed 128 times
2 Answers
2
you can use zfill
demo:
>>> [ str(x).zfill(5) for x in range(10) ]
['00000', '00001', '00002', '00003', '00004', '00005', '00006', '00007', '00008', '00009']
demo with IT
:
>>>[ 'IT'+str(x).zfill(5) for x in range(10) ]
['IT00000', 'IT00001', 'IT00002', 'IT00003', 'IT00004', 'IT00005', 'IT00006', 'IT00007', 'IT00008', 'IT00009']
you can also use format
:
>>> [ '{}{:05d}'.format('IT',x) for x in range(10) ]
['IT00000', 'IT00001', 'IT00002', 'IT00003', 'IT00004', 'IT00005', 'IT00006', 'IT00007', 'IT00008', 'IT00009']

Hackaholic
- 19,069
- 5
- 54
- 72