Life is short, you need Python

ljust rjust center

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

print '|','*'.ljust(10),'|'
print '|','*'.ljust(10,'-'),'|'
print '|','*'.rjust(10,'-'),'|'
print '|','*'.center(10,'-'),'|'

for a in range(16):
    
print 'a = '.ljust(5), repr(a).ljust(10), 'b = '.ljust(5), repr(a * 2)

attention: '|'与'*'为号,'*'与ljust()间为

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

输出结果:

| *          |
| *--------- |
| ---------* |
| ----*----- |
=   1          b =   2
=   2          b =   4
=   3          b =   6
=   4          b =   8
=   5          b =   10
posted @ 2010-06-24 11:12  runfox545  阅读(234)  评论(0编辑  收藏  举报
白月黑羽 Python教程 白月黑羽Python