python中右对齐的方法
这篇文章将为大家详细讲解有关python中右对齐的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
例如,有一个字典如下:
>>>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:botoourl:http://www.123.compage:88isNonProfit:trueaddress:china>>>forkindic:print(k.rjust(d),":",dic[k])name:botoourl:http://www.123.compage:88isNonProfit:trueaddress:china>>>forkindic:print(k.center(d),":",dic[k])name:botoourl:http://www.123.compage:88isNonProfit:trueaddress:china>>>
关于 str.ljust()的用法还有这样的;
>>>s="adc">>>s.ljust(20,"+")'adc+++++++++++++++++'>>>s.rjust(20)'adc'>>>s.center(20,"+")'++++++++adc+++++++++'>>>
关于python中右对齐的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。