python 对字符串进行补零操作
python 字符串补 0作者:解琛时间:2020 年 8 月 14 日一、ljust原字符串左侧对齐, 右侧补零。格式:str.ljust(width, '0')案例>>> "123".ljust(16, "0")'1230000000000000'二、rjust原字符串右侧对齐, 左侧补零。格式:str.rjust(width, '0')案例>>> "123
·
python 字符串补 0
作者:解琛
时间:2020 年 8 月 14 日
一、ljust
原字符串左侧对齐, 右侧补零。
格式:
str.ljust(width, '0')
案例
>>> "123".ljust(16, "0")
'1230000000000000'
二、rjust
原字符串右侧对齐, 左侧补零。
格式:
str.rjust(width, '0')
案例
>>> "123".rjust(16, "0")
'0000000000000123'
三、zfill
左侧补零。
格式:
str.zfill(width)
案例
>>> "123".zfill(16)
'0000000000000123'
四、%
左侧补零。
案例
>>> '%016d' % 123
'0000000000000123'
更多推荐
所有评论(0)