python怎么右对齐


本文摘自php中文网,作者爱喝马黛茶的安东尼,侵删。

例如,有一个字典如下:

1

2

3

4

5

6

7

>>> 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() 居中的方法有序列的输出。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

>>> 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()的用法还有这样的;

1

2

3

4

5

6

7

8

>>> s = "adc"

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

'adc+++++++++++++++++'

>>> s.rjust(20)

'                 adc'

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

'++++++++adc+++++++++'

>>>

以上就是python怎么右对齐的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python面向对象的实例讲解

Python实现二维数组输出为图片_Python

利用Python将图片转换成excel文档格式详解

Python中字典是怎么使用的?Python字典的使用

Python是脚本语言吗

Python利用openpyxl库遍历sheet的实例

mac 正确地配置 scipy 开发环境

Python是什么公司开发

matplotlib中对图形颜色和线条的填充

Python怎么用idle

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...