python右對(duì)齊的實(shí)例方法
例如,有一個(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)!
相關(guān)文章:
1. Python 制作查詢商品歷史價(jià)格的小工具2. python 使用事件對(duì)象asyncio.Event來同步協(xié)程的操作3. ASP基礎(chǔ)知識(shí)VBScript基本元素講解4. Python3 json模塊之編碼解碼方法講解5. 使用Python webdriver圖書館搶座自動(dòng)預(yù)約的正確方法6. Python sublime安裝及配置過程詳解7. Python 合并拼接字符串的方法8. Linux刪除系統(tǒng)自帶版本Python過程詳解9. Python 利用Entrez庫(kù)篩選下載PubMed文獻(xiàn)摘要的示例10. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條

網(wǎng)公網(wǎng)安備