In Python 2 you can do:
(在Python 2中,您可以执行以下操作:)
print "%02d" % (1,)
Basically % is like printf
or sprintf
.
(基本上%就像printf
或sprintf
。)
For Python 3.+ the same behavior can be achieved with:
(对于Python 3. +,可以通过以下方式实现相同的行为:)
print("{:02d}".format(1))
For Python 3.6+ the same behavior can be achieved with f-strings:
(对于Python 3.6+,可以使用f字符串实现相同的行为:)
print(f"{1:02d}")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…