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

Python天天美味(5) - ljust rjust center(转)print '|','*'.ljust(10),'|'
Python天天美味(5) - ljust rjust center(转)
print '|','*'.ljust(10,'-'),'|'
Python天天美味(5) - ljust rjust center(转)
print '|','*'.rjust(10,'-'),'|'
Python天天美味(5) - ljust rjust center(转)
print '|','*'.center(10,'-'),'|'
Python天天美味(5) - ljust rjust center(转)
Python天天美味(5) - ljust rjust center(转)
for a in range(16):
Python天天美味(5) - ljust rjust center(转)    
print 'a = '.ljust(5), repr(a).ljust(10), 'b = '.ljust(5), repr(a * 2)


输出结果:

Python天天美味(5) - ljust rjust center(转)| *          |
Python天天美味(5) - ljust rjust center(转)
| *--------- |
Python天天美味(5) - ljust rjust center(转)
| ---------* |
Python天天美味(5) - ljust rjust center(转)
| ----*----- |
Python天天美味(5) - ljust rjust center(转)
=   1          b =   2
Python天天美味(5) - ljust rjust center(转)
=   2          b =   4
Python天天美味(5) - ljust rjust center(转)
=   3          b =   6
Python天天美味(5) - ljust rjust center(转)
=   4          b =   8
Python天天美味(5) - ljust rjust center(转)
=   5          b =   10

相关文章: