python右对齐的实例方法
作者:爱喝马黛茶的安东尼 时间:2022-01-15 20:59:52
例如,有一个字典如下:
>>> dic = {
"name": "botoo",
"url": "//www.jb51.net",
"page": "88",
"isNonProfit": "true",
"address": "china",
}
想要得到的输出结果如下:
name:botoo
url:https:www.jb51.net
page:88
isNonProfit:ture
address:china
首先获取字典的最大值max(map(len, dic.keys()))
然后使用
Str.rjust() 右对齐
或者
Str.ljust() 左对齐
或者
Str.center() 居中的方法有序列的输出。
>>> dic = {
"name": "botoo",
"url": "//www.jb51.net",
"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 : //www.jb51.net
page : 88
isNonProfit : true
address : china
>>> for k in dic:
print(k.rjust(d),":",dic[k])
name : botoo
url : //www.jb51.net
page : 88
isNonProfit : true
address : china
>>> for k in dic:
print(k.center(d),":",dic[k])
name : botoo
url : //www.jb51.net
page : 88
isNonProfit : true
address : china
>>>
关于 str.ljust()的用法还有这样的;
>>> s = "adc"
>>> s.ljust(20,"+")
'adc+++++++++++++++++'
>>> s.rjust(20)
'adc'
>>> s.center(20,"+")
'++++++++adc+++++++++'
>>>
知识点扩展:
python中对字符串的对齐操作
ljust()、rjust() 和 center()函数分别表示左对齐、右对齐、居中对齐
str.ljust(width[, fillchar]):左对齐,width -- 指定字符串长度,fillchar -- 填充字符,默认为空格;
str.rjust(width[, fillchar]):右对齐,width -- 指定字符串长度,fillchar -- 填充字符,默认为空格;
str.center(width[, fillchar]):居中对齐,width -- 字符串的总宽度,fillchar -- 填充字符,默认为空格。
test = 'hello world'
print(test.ljust(20))
print(test.ljust(20, '*'))
print(test.rjust(20, '*'))
print(test.center(20, '*'))
print(test.center(20))
#输出结果如下:
hello world*********
*********hello world
****hello world*****
hello world
来源:https://www.py.cn/faq/python/14305.html
标签:python,右对齐
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python数学形态学实例分析
2022-11-22 22:32:50
![](https://img.aspxhome.com/file/2023/6/92526_0s.png)
这些有问题的细节设计
2009-04-20 12:47:00
![](https://img.aspxhome.com/file/UploadPic/20094/20/wangwang-84s.png)
手把手教你利用opencv实现人脸识别功能(附源码+文档)
2022-01-24 23:55:37
![](https://img.aspxhome.com/file/2023/6/93456_0s.jpg)
Go 值传递与引用传递的方法
2023-06-25 03:11:11
![](https://img.aspxhome.com/file/2023/3/89023_0s.png)
Python基础 括号()[]{}的详解
2023-07-22 07:35:39
ASP编写计数器的优化方法
2009-01-21 19:46:00
快速解决vue.js 模板和jinja 模板冲突的问题
2023-04-04 12:49:59
![](https://img.aspxhome.com/file/2023/5/81785_0s.jpg)
Python访问Redis的详细操作
2022-03-13 13:22:25
![](https://img.aspxhome.com/file/2023/5/98785_0s.png)
Python导入模块包原理及相关注意事项
2023-01-26 04:46:31
![](https://img.aspxhome.com/file/2023/6/97266_0s.png)
在JavaScript中,为什么要尽可能使用局部变量?
2009-03-01 12:38:00
![](https://img.aspxhome.com/file/UploadPic/20093/1/variable_write_performance2-17s.png)
php for 循环语句使用方法详细说明
2023-11-17 21:43:21
《Python学习手册》学习总结
2021-09-17 08:55:01
![](https://img.aspxhome.com/file/2023/5/79535_0s.jpg)
Python中seaborn库之countplot的数据可视化使用
2023-08-10 20:38:56
![](https://img.aspxhome.com/file/2023/6/62026_0s.png)
Python使用淘宝API查询IP归属地功能分享
2021-02-11 20:37:29
PHP的mysqli_thread_id()函数讲解
2023-06-13 10:09:43
![](https://img.aspxhome.com/file/2023/3/55453_0s.png)
pytorch 实现删除tensor中的指定行列
2023-05-22 14:26:26
使用javascript提交form表单方法汇总
2023-08-23 09:03:48
10个杀手级应用的Python自动化脚本
2023-10-06 14:01:53
我是如何从玩Photoshop变成老板的
2008-04-10 11:33:00
![](https://img.aspxhome.com/file/UploadPic/20084/10/2008410114044665s.jpg)
如何让Firefox2和Firefox3在Windows下共存并同时运行?
2008-06-01 15:50:00
![](https://img.aspxhome.com/file/UploadPic/20086/1/200861155756587s.png)