Home > Enterprise >  Ascend 000 to 999
Ascend 000 to 999

Time:04-18

I am trying to repeatedly ascend integers in the format of 000, 001, I have attempted to do the following

>>> for x in range(000, 999):
        ...

BUT, it returns..

1
2
3
4...

CodePudding user response:

You can do it like this.

for i in range(0, 1000):
    print(f"{i:03}")

Addendum: If you simply want to print it, you need to convert it to string. You cannot have 001 in an integer format.

Edit: change the range to end at 1000, if you want to print 999.

CodePudding user response:

zfill is your friend here, it will add the zeros for you:

https://www.w3schools.com/python/ref_string_zfill.asp

  • Related