久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python右對齊的實例方法

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

例如,有一個字典如下:

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

想要得到的輸出結果如下:

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

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

然后使用

Str.rjust() 右對齊

或者

Str.ljust() 左對齊

或者

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>>>

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

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

知識點擴展:

python中對字符串的對齊操作

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

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

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

到此這篇關于python右對齊的實例方法的文章就介紹到這了,更多相關python中如何右對齊內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 云林县| 栾城县| 定西市| 嵩明县| 高安市| 白玉县| 天水市| 湾仔区| 黔东| 天峨县| 黔西县| 武夷山市| 镇赉县| 天等县| 彰武县| 临武县| 女性| 乐安县| 柞水县| 津南区| 横峰县| 始兴县| 霸州市| 乐安县| 三亚市| 凤凰县| 疏勒县| 桂平市| 大姚县| 武山县| 蓝山县| 信丰县| 朝阳市| 顺平县| 辽宁省| 灵寿县| 平武县| 宁陵县| 朝阳市| 商城县| 秀山|