How do I format a number with a variable number of digits in Python?

后端 未结 7 897
攒了一身酷
攒了一身酷 2020-12-07 12:23

Say I wanted to display the number 123 with a variable number of padded zeroes on the front.

For example, if I wanted to display it in 5 digits I would have digits =

7条回答
  •  没有蜡笔的小新
    2020-12-07 12:55

    For those who want to do the same thing with python 3.6+ and f-Strings this is the solution.

    width = 20
    py, vg = "Python", "Very Good"
    print(f"{py:>{width}s} : {vg:>{width}s}")
    

提交回复
热议问题