Display number with leading zeros

后端 未结 16 2661
我在风中等你
我在风中等你 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:18

    In Python 2 (and Python 3) you can do:

    print "%02d" % (1,)
    

    Basically % is like printf or sprintf (see docs).


    For Python 3.+, the same behavior can also be achieved with format:

    print("{:02d}".format(1))
    

    For Python 3.6+ the same behavior can be achieved with f-strings:

    print(f"{1:02d}")
    

提交回复
热议问题