Display number with leading zeros

后端 未结 16 2528
我在风中等你
我在风中等你 2020-11-22 03:00

Given:

a = 1
b = 10
c = 100

How do I display a leading zero for all numbers with less than two digits?

This is the output I\'m expe

16条回答
  •  孤城傲影
    2020-11-22 03:23

    In Python 2.6+ and 3.0+, you would use the format() string method:

    for i in (1, 10, 100):
        print('{num:02d}'.format(num=i))
    

    or using the built-in (for a single number):

    print(format(i, '02d'))
    

    See the PEP-3101 documentation for the new formatting functions.

提交回复
热议问题