python怎么右对齐

互联网 19-8-1

例如,有一个字典如下:

>>> 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怎么右对齐的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: 右对齐
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:python学会了可以干什么

相关资讯