概述
例如,有一个字典如下:
>>> dic = {
"name": "botoo",
"url": "http://www.123.com",
"page": "88",
"isNonProfit": "true",
"address": "china",
}
登录后复制
想要得到的输出结果如下:
相关推荐:《Python视频教程》
首先获取字典的最大值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怎么右对齐的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是谦让火为你收集整理的python怎么右对齐的全部内容,希望文章能够帮你解决python怎么右对齐所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复