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'

python-字符串处理-字符串补全填充固定长度(补0)的三种方法

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐