首页 > 编程语言 > 详细

python3----ljust rjust center

时间:2018-01-11 19:04:42      阅读:262      评论:0      收藏:0      [点我收藏+]

Python中打印字符串时可以调用ljust(左对齐),rjust(右对齐),center(中间对齐)来输出整齐美观的字符串,使用起来非常简单,包括使用第二个参数填充(默认为空格)。看下面的例子就会明白了:

1 print(‘|‘, ‘*‘.ljust(10), ‘|‘)
2 print(‘|‘, ‘*‘.ljust(10, ‘-‘), ‘|‘)
3 print(‘|‘, ‘*‘.rjust(10, ‘-‘), ‘|‘)
4 print(‘|‘, ‘*‘.center(10, ‘-‘), ‘|‘)
5 
6 for a in range(1, 6):
7     print(‘a = ‘.ljust(5), repr(a).ljust(10), ‘b = ‘.ljust(5), repr(a * 2))

attention: ‘|‘与‘*‘为逗号,‘*‘与ljust()间为点号

repr函数和反引号用来获取对象的可打印的表示形式

输出结果:

| *          |
| *--------- |
| ---------* |
| ----*----- |
a =   1          b =   2
a =   2          b =   4
a =   3          b =   6
a =   4          b =   8
a =   5          b =   10

 

python3----ljust rjust center

原文:https://www.cnblogs.com/jonm/p/8269921.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!