Display number with leading zeros

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

    x = [1, 10, 100]
    for i in x:
        print '%02d' % i
    

    results in:

    01
    10
    100
    

    Read more information about string formatting using % in the documentation.

提交回复
热议问题