导语:
本文主要介绍了关于python中如何右对齐的相关知识,包括word居中右对齐,以及居中右对齐怎么设置这些编程知识,希望对大家有参考作用。
例如,有一个字典如下:
>>> 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的值
>>>
>>> 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中如何判断值是否为008/27
- ♥ 如何在vim中使用python10/17
- ♥ 如何检查python是否安装10/06
- ♥ python中将序列转换为数据框的两种方法08/11
- ♥ python标识符是什么意思?09/08
- ♥ 如何使用python的str函数01/03
内容反馈