当前位置:主页 > 软件编程 > Python代码 >

Python补齐字符串长度的实例

时间:2021-06-02 08:22:55 | 栏目:Python代码 | 点击:

如下所示:

ljust(len,str)字符向左对齐,用str补齐长度

rjust(len,str)字符向右对齐,用str补齐长度

rjust(len,str)字符中间对齐,用str补齐长度


print 'bbb'.ljust(10,'a')

输出:bbbaaaaaaa

print 'bbb'.rjust(10,'a')

输出:aaaaaaabbb

print 'bbb'.center(10,'a')

输出:aaabbbaaaa


zfill(width)指定字符串长度,右对齐,前面补充0


print '2'.zfill(5)

输出:00002

您可能感兴趣的文章:

相关文章