码迷,mamicode.com
首页 > 编程语言 > 详细

python3----ljust rjust center

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

标签:对齐   int   body   attention   python   enter   blog   获取   pytho   

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

标签:对齐   int   body   attention   python   enter   blog   获取   pytho   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!