In Python, how to specify a format when converting int to string?

后端 未结 5 1923
终归单人心
终归单人心 2020-12-29 05:55

In Python, how do I specify a format when converting int to string?

More precisely, I want my format to add leading zeros to have a string with constant length. For

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 06:39

    Try formatted string printing:

    print "%04d" % 1 Outputs 0001

提交回复
热议问题