当前位置: 首页 > 图文教程 > 脚本技术 > Python > Python ljust rjust center输出

Python
Python 文件操作实现代码
动态创建类实例代码
python 中文字符串的处理实现代码
Python 匹配任意字符(包括换行符)的正则表达式写法
Python 开发Activex组件方法
Python+Django在windows下的开发环境配置图解
python 文件和路径操作函数小结
python 快速排序代码
Python2.5/2.6实用教程 入门基础篇
Python3 入门教程 简单但比较不错
Python 元类使用说明

Python ljust rjust center输出


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-11   浏览: 143 ::
收藏到网摘: n/a

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

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

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

输出结果:
| * |
| *--------- |
| ---------* |
| ----*----- |
a = 1 b = 2
a = 2 b = 4
a = 3 b = 6
a = 4 b = 8
a = 5 b = 10