python基础===对字符串进行左右中对齐
2018-08-12 17:43
489 查看
例如,有一个字典如下:
>>> dic = {
"name": "botoo",
"url": "http://www.123.com",
"page": "88",
"isNonProfit": "true",
"address": "china",
}
想要得到的输出结果如下:
首先 获取字典 的 最大值 max(map(len, dic.keys()))
然后使用
Str.rjust() 右对齐
或者
Str.ljust() 左对齐
或者
Str.center() 居中的方法有序列的输出。
>>> dic = { "name": "botoo", "url": "http://www.123.com", "page": "88", "isNonProfit": "true", "address": "china", } >>> >>> d = max(map(len, dic.keys())) #获取key的最大值 >>> >>> for k in dic: print(k.ljust(d),":",dic[k]) name : botoo url : http://www.123.com page : 88 isNonProfit : true address : china >>> for k in dic: print(k.rjust(d),":",dic[k]) name : botoo url : http://www.123.com page : 88 isNonProfit : true address : china >>> for k in dic: print(k.center(d),":",dic[k]) name : botoo url : http://www.123.com page : 88 isNonProfit : true address : china >>>
关于 str.ljust()的用法还有这样的;
>>> s = "adc" >>> s.ljust(20,"+") 'adc+++++++++++++++++' >>> s.rjust(20) ' adc' >>> s.center(20,"+") '++++++++adc+++++++++' >>>
相关文章推荐
- 利用Python进行数据分析(15) pandas基础: 字符串操作
- Python3基础 os.path.dirname 对一个路径字符串进行处理 返回路径
- Python3基础 os.path.splitext 对一个带有扩展名的文件名字符串 进行分割 返回 文件名+扩展名
- Python3 Tkinter基础 Label justify=LEFT 多行字符串左对齐
- 利用Python进行数据分析(15) pandas基础: 字符串操作
- python基础--字符串常用函数
- Python中字符串对齐方法介绍
- Python3 Tkinter基础 窗体的布局 使用grid(row,column)进行
- Python3基础 print括号中 字符串乘以一个整数 输出多个相同的字符串
- python 基础-----数字,字符串,if while 循环 数据类型的转换简单介绍
- 利用Python进行数据分析 pandas基础: 处理缺失数据
- Python3基础 str center 左右填充空格,达到指定长度,使文本居中
- python-基础知识之字符串操作
- Python基础--使用字符串
- 扩展字符串左右对齐方法
- Python基础(2)_数字和字符串类型
- 【Python】Python中中文的字符串格式化对齐
- python基础学习(十)字符串
- python基础知识-元组和字符串
- Python基础字符串知识点小结