but*_*tch 78 python string string-formatting python-2.7
你能用str.format函数显示带前导零的整数值吗?
输入示例:
"{0:some_format_specifying_width_3}".format(1)
"{0:some_format_specifying_width_3}".format(10)
"{0:some_format_specifying_width_3}".format(100)
Run Code Online (Sandbox Code Playgroud)
期望的输出:
"001"
"010"
"100"
Run Code Online (Sandbox Code Playgroud)
我知道,无论zfill和%基于格式(如'%03d' % 5)可以做到这一点.但是,我想要一个解决方案,用于str.format保持我的代码干净和一致(我也使用datetime属性格式化字符串),并扩展我对格式规范迷你语言的了解.
And*_*ark 190
>>> "{0:0>3}".format(1)
'001'
>>> "{0:0>3}".format(10)
'010'
>>> "{0:0>3}".format(100)
'100'
Run Code Online (Sandbox Code Playgroud)
说明:
{0 : 0 > 3}
? ? ? ?
? ? ? ?? Width of 3
? ? ?? Align Right
? ?? Fill with '0'
?? Element index
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54311 次 |
| 最近记录: |