日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python右对齐函数_python右对齐的实例方法

發布時間:2025/5/22 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python右对齐函数_python右对齐的实例方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

例如,有一個字典如下:

>>> dic = {

"name": "botoo",

"url": "//www.jb51.net",

"page": "88",

"isNonProfit": "true",

"address": "china",

}

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

name:botoo

url:https:www.jb51.net

page:88

isNonProfit:ture

address: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 : botoo

url : //www.jb51.net

page : 88

isNonProfit : true

address : china

>>> for k in dic:

print(k.rjust(d),":",dic[k])

name : botoo

url : //www.jb51.net

page : 88

isNonProfit : true

address : china

>>> for k in dic:

print(k.center(d),":",dic[k])

name : botoo

url : //www.jb51.net

page : 88

isNonProfit : 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右对齐函数_python右对齐的实例方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。