将数字转换为python中带有前缀0s的格式化字符串

plu*_*uke 0 python python-3.x

我想将python中的数字转换为3个字符的字符串,前缀为0.例如:

8  = "008"
12 = "012"
6  = "006"
Run Code Online (Sandbox Code Playgroud)

谢谢

Jon*_*nts 6

只需使用:

format(number, '03')
Run Code Online (Sandbox Code Playgroud)


unu*_*tbu 5

我的名字是格式。字符串格式。

>>> '{:03d}'.format(7)
'007'
Run Code Online (Sandbox Code Playgroud)