教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 python怎么右对齐

python怎么右对齐

发布时间:2020-10-11   编辑:jiaochengji.com
教程集为您提供python怎么右对齐等资源,欢迎您收藏本站,我们将为您提供最新的python怎么右对齐资源

例如,有一个字典如下:

>>> dic = {
"name": "botoo",
"url": "http://www.123.com",
"page": "88",
"isNonProfit": "true",
"address": "china",
}

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

qq.png

相关推荐:《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怎么右对齐的详细内容,更多请关注教程集其它相关文章!

  • 本文原创发布教程集,转载请注明出处,感谢您的尊重!
  • 您可能感兴趣的文章:
    php怎么把信息对齐输出
    python怎么右对齐
    python怎么格式化输出
    python的format怎么用
    如何实现python3中的字符串对齐?
    python里百分号什么意思
    在python中%是什么意思
    photoshop基础常用对齐技巧心得分享一览
    Illustrator像素级完美绘制经验技巧分享
    电脑日期时间显示不对怎么办?怎么解决

    [关闭]
    ~ ~