码迷,mamicode.com
首页 > 其他好文 > 详细

利用 ljust rjust center函数打印整齐的字符串

时间:2014-12-21 18:09:33      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:ljust   center   rjust   

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

print ‘|‘,‘*‘.ljust(10),‘|‘

print ‘|‘,‘*‘.ljust(10,‘-‘),‘|‘

print ‘|‘,‘*‘.rjust(10,‘-‘),‘|‘

print ‘|‘,‘*‘.center(10,‘-‘),‘|‘

输出效果:

| *          |
| *--------- |
| ---------* |
| ----*----- |


利用 ljust rjust center函数打印整齐的字符串

标签:ljust   center   rjust   

原文地址:http://leboit.blog.51cto.com/1465210/1592307

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