网络编程
位置:首页>> 网络编程>> Python编程>> Python补齐字符串长度的实例

Python补齐字符串长度的实例

作者:橙子伟  发布时间:2023-09-24 21:51:43 

标签: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

来源:https://blog.csdn.net/qq_35711912/article/details/80477950

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com