Skip to content
Advertisement

How do I repeat serial numbers from 000 to 999 in Python [closed]

I want to iterate serial numbers from 000 to 999 in Python and save them to a txt file

import os
os.system ('clear')

lis9 = 01000
while lis9 < 01099 :
   lis9 = lis9+1
   print (lis9)

How do I start from a specific number and end at a specific number? The numbers start from zero

Each number begins on a new line

For example

01000
01001
.
01010
.
.
01099

Advertisement

Answer

Use f string

with open('out.txt','w') as f:
    for n in range(0,1000):
        f.write(f'{n:03}n')

output (‘out.txt’)

000
001
002
003
...
996
997
998
999
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement