最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python右對齊的實(shí)例方法

 更新時(shí)間:2020年07月05日 11:15:59   作者:愛喝馬黛茶的安東尼  
在本篇文章里小編給大家整理的是關(guān)于python右對齊的實(shí)例方法,有需要的朋友們可以學(xué)習(xí)參考下。

例如,有一個(gè)字典如下:

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

想要得到的輸出結(jié)果如下:

name:botoo
url:https:www.fzitv.net
page:88
isNonProfit:ture
address:china

首先獲取字典的最大值max(map(len, dic.keys()))

然后使用

Str.rjust() 右對齊

或者

Str.ljust() 左對齊

或者

Str.center() 居中的方法有序列的輸出。

>>> dic = {
  "name": "botoo",
  "url": "http://www.fzitv.net",
  "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     : //www.fzitv.net
page    : 88
isNonProfit : true
address   : china
>>> for k in dic:
  print(k.rjust(d),":",dic[k])
   
    name : botoo
    url : //www.fzitv.net
    page : 88
isNonProfit : true
  address : china
>>> for k in dic:
  print(k.center(d),":",dic[k])
   
  name  : botoo
  url   : //www.fzitv.net
  page  : 88
isNonProfit : true
 address  : china
>>>

關(guān)于 str.ljust()的用法還有這樣的;

>>> s = "adc"
>>> s.ljust(20,"+")
'adc+++++++++++++++++'
>>> s.rjust(20)
'adc'
>>> s.center(20,"+")
'++++++++adc+++++++++'
>>>

知識點(diǎn)擴(kuò)展:

python中對字符串的對齊操作

ljust()、rjust() 和 center()函數(shù)分別表示左對齊、右對齊、居中對齊

str.ljust(width[, fillchar]):左對齊,width -- 指定字符串長度,fillchar -- 填充字符,默認(rèn)為空格;
str.rjust(width[, fillchar]):右對齊,width -- 指定字符串長度,fillchar -- 填充字符,默認(rèn)為空格;
str.center(width[, fillchar]):居中對齊,width -- 字符串的總寬度,fillchar -- 填充字符,默認(rèn)為空格。

test = 'hello world'
print(test.ljust(20))
print(test.ljust(20, '*'))
print(test.rjust(20, '*'))
print(test.center(20, '*'))
print(test.center(20))
 
#輸出結(jié)果如下:
hello world*********
*********hello world
****hello world*****
  hello world   

到此這篇關(guān)于python右對齊的實(shí)例方法的文章就介紹到這了,更多相關(guān)python中如何右對齊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

大洼县| 阳西县| 璧山县| 贵港市| 巴南区| 鄂伦春自治旗| 玉山县| 博野县| 孝昌县| 潼关县| 张家港市| 疏附县| 金坛市| 丰顺县| 巴楚县| 威远县| 郴州市| 防城港市| 郸城县| 康定县| 兴国县| 吉安市| 敖汉旗| 永修县| 惠安县| 克山县| 岢岚县| 弥勒县| 襄汾县| 潍坊市| 宽甸| 荃湾区| 广汉市| 全椒县| 扎赉特旗| 环江| 雷山县| 雅安市| 边坝县| 贺州市| 稷山县|