例如,有一个字典如下:
>>>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的值 >>> >>>forkindic: print(k.ljust(d),":",dic[k]) name:botoo url:http://www.123.com page:88 isNonProfit:true address:china >>>forkindic: print(k.rjust(d),":",dic[k]) name:botoo url:http://www.123.com page:88 isNonProfit:true address:china >>>forkindic: 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+++++++++' >>>原文来自:https://www.py.cn
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容