国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:40日期:2022-07-18 16:18:28

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

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

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

name:botoourl:https:www.jb51.netpage:88isNonProfit:tureaddress:china

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

然后使用

Str.rjust() 右對(duì)齊

或者

Str.ljust() 左對(duì)齊

或者

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

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

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

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

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

python中對(duì)字符串的對(duì)齊操作

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

str.ljust(width[, fillchar]):左對(duì)齊,width -- 指定字符串長(zhǎng)度,fillchar -- 填充字符,默認(rèn)為空格;str.rjust(width[, fillchar]):右對(duì)齊,width -- 指定字符串長(zhǎng)度,fillchar -- 填充字符,默認(rèn)為空格;str.center(width[, fillchar]):居中對(duì)齊,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右對(duì)齊的實(shí)例方法的文章就介紹到這了,更多相關(guān)python中如何右對(duì)齊內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 体育| 威远县| 哈尔滨市| 光泽县| 安平县| 苏尼特右旗| 忻城县| 梁平县| 丽水市| 丁青县| 盘山县| 辽源市| 黄浦区| 南岸区| 四川省| 贵德县| 财经| 大城县| 宁海县| 邵阳县| 曲阜市| 肇东市| 芷江| 旅游| 靖安县| 东山县| 堆龙德庆县| 临江市| 兴山县| 扶风县| 车险| 金沙县| 自治县| 巧家县| 辉南县| 绵竹市| 普兰店市| 锡林郭勒盟| 东平县| 呼和浩特市| 剑阁县|