python3----ljust rjust center

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
原文地址:https://www.cnblogs.com/jonm/p/8269921.html