Generate numbers with 3 digits

后端 未结 3 1288
旧时难觅i
旧时难觅i 2021-02-07 16:35

I wanted to generate numbers starting from 000 to 120 in sequence. I know you can generate numbers from 0 to 120 by using a loop.But I want all the numbers to have 3 digits.

相关标签:
3条回答
  • 2021-02-07 17:12
    print [format(item, "03d") for item in xrange(121)]
    
    0 讨论(0)
  • 2021-02-07 17:22

    python 3.6

    for i in range(121):
        print(f'{i:03}')
    
    0 讨论(0)
  • 2021-02-07 17:30
    ["{0:03}".format(i) for i in range(121)]
    

    or

    ["%03d" % i for i in range(121)]
    

    To print:

    print "\n".join(<either of the above expressions>)
    

    More terse in Python 3.6+:

    [f"{i:03}" for i in range(121)]
    
    0 讨论(0)
提交回复
热议问题