python数字、字符串对齐
2015-10-21 15:31
681 查看
#!/usr/bin/python
data1=5
data2=10
str1 ='iot_history_%-2d'%data1 + ' ok'
str2 ='iot_history_%-2d'%data2 + ' ok'
print str1
print str2
输出:
iot_history_5 ok
iot_history_10 ok
上面的 * 对应 width,d 对应 data,输出的 data 占 width 个字符长度
或者直接
这样就是表明占4位,在%后面加一个 - 就是左对齐,位数可以通过len(max())获得
字符串对齐:
word="hello"
print word.center(20)
#居中输出,总共20个字符,word左右两侧各输出5个空格
print word.center(20,"*")
#居中输出,总共20个字符,word左右两侧各输出5个"*"号
print word.ljust(0) #左对齐输出
print word.rjust(20) #右对齐输出,总共20个字符,word占10个字符,因此左侧填充一个空格再输出word
print "%20s"%word #类似于word.rjust(30
data1=5
data2=10
str1 ='iot_history_%-2d'%data1 + ' ok'
str2 ='iot_history_%-2d'%data2 + ' ok'
print str1
print str2
输出:
iot_history_5 ok
iot_history_10 ok
或者直接
字符串对齐:
word="hello"
print word.center(20)
#居中输出,总共20个字符,word左右两侧各输出5个空格
print word.center(20,"*")
#居中输出,总共20个字符,word左右两侧各输出5个"*"号
print word.ljust(0) #左对齐输出
print word.rjust(20) #右对齐输出,总共20个字符,word占10个字符,因此左侧填充一个空格再输出word
print "%20s"%word #类似于word.rjust(30
相关文章推荐
- Python logging模块详解
- python xlwt写excel格式控制
- 旧文备份:Python国际化支持
- LeetCode----Balanced Binary Tree
- python 的Dict
- python读取xml文件
- python---打印输出/中文输出
- 【Python】调用WPS V9 API,实现Word转PDF
- Python正则表达式匹配操作re模块
- Python正则表达式匹配操作re模块
- Python-- easy_install 的安装
- [Python]Pip的安装以及简单的使用
- python----小数点精度控制round()
- python JSON性能测试与simplejson对比
- python 模块安装
- Python爬虫:动态网页抓取淘宝“淘女郎”照片
- what yield does in python?
- Python面向对象详解
- Python notes
- Python 正则表达式(分组)