当前位置:   article > 正文

python左右同时对齐_python基础===对字符串进行左右中对齐

python中左右对齐符号

例如,有一个字典如下:

>>> dic = {

"name": "botoo",

"url": "http://www.123.com",

"page": "88",

"isNonProfit": "true",

"address": "china",

}

想要得到的输出结果如下:

1208761-20180812173845052-274928454.png

首先 获取字典 的 最大值 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 indic:print(k.ljust(d),":",dic[k])

name : botoo

url : http://www.123.com

page :88isNonProfit : true

address : china>>> for k indic:print(k.rjust(d),":",dic[k])

name : botoo

url : http://www.123.com

page :88isNonProfit : true

address : china>>> for k indic:print(k.center(d),":",dic[k])

name : botoo

url : http://www.123.com

page :88isNonProfit : true

address : china>>>

关于 str.ljust()的用法还有这样的;

>>> s = "adc"

>>> s.ljust(20,"+")'adc+++++++++++++++++'

>>> s.rjust(20)'adc'

>>> s.center(20,"+")'++++++++adc+++++++++'

>>>

顺便提一下

如果有任何问题,你可以在这里找到我 ,软件测试交流qq群,209092584

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/294545
推荐阅读
相关标签
  

闽ICP备14008679号