Python?format字符串格式化函數(shù)的使用
一、簡(jiǎn)介
從Python2.6開(kāi)始,新增了str.format(),它增強(qiáng)了字符串格式化的功能?;菊Z(yǔ)法是通過(guò) {} 和 : 來(lái)代替以前的 % 占位符。
二、占位符%方式
字符串格式符號(hào)用法如下

舉個(gè)例子:
name = 'sugar'
age = 21
print("His name is %s, and he is %d year old." %(name, age))
結(jié)果
His name is sugar, and he is 21 year old.
其他格式化輔助操作指令如下,其中用的比較多的就是使用0來(lái)補(bǔ)零,和控制小數(shù)位數(shù)的.

舉個(gè)例子:
price = 23.1999
obj = 'milk'
print("The %s's price is %03f" %(obj, price)) # 前面補(bǔ)三個(gè)零
print("The %s's price is %3.0f" %(obj, price)) # 最小總占位長(zhǎng)度為3,控制輸出0個(gè)小數(shù)
print("The %s's price is %3.3f" %(obj, price)) # 最小總占位長(zhǎng)度為3,控制輸出3個(gè)小數(shù)
print("The %s's price is %5.4f" %(obj, price)) # 最小總占位長(zhǎng)度為5,控制輸出4個(gè)小數(shù)
結(jié)果:
The milk's price is 23.199900 The milk's price is 23 The milk's price is 23.200 The milk's price is 23.1999
三、format格式化方式
字符串format格式化的四種方式
1、使用默認(rèn)位置方式
格式:string{}.format(x1, x2)
舉個(gè)例子
price = 23.1999
obj = 'milk'
print("The {}'s price is {}".format(obj, price))
結(jié)果如下
The milk's price is 23.1999
2、使用指定位置方式
格式:string{0}.format(x1, x2)
舉個(gè)例子
price = 23.1999
obj = 'milk'
print("The {0}'s price is {1}".format(obj, price))
結(jié)果如下
The milk's price is 23.1999
3、使用列表方式
其實(shí)這種方式就相當(dāng)于前兩種使用默認(rèn)位置和使用指定位置的方式,只不過(guò)這里需要使用*對(duì)列表進(jìn)行解包,舉個(gè)例子
price = 23.1999
obj = 'milk'
info = [obj, price]
print("The {}'s price is {}".format(*info)) # 對(duì)info進(jìn)行解包
結(jié)果如下
The milk's price is 23.1999
4、使用字典的鍵值對(duì)方式
格式:string(key).format(key=value)
舉個(gè)例子,當(dāng)然也可以用**對(duì)字典進(jìn)行解包
price = 23.1999
obj = 'milk'
print("The {name}'s price is {pri}".format(name=obj, pri=price))
# 更進(jìn)一步,對(duì)字典進(jìn)行解包
dic = {'name':'milk', 'pri':23.1999}
print("The {name}'s price is {pri}".format(**dic))
結(jié)果如下
The milk's price is 23.1999
The milk's price is 23.1999
5、其他數(shù)字格式化的方式

需要注意的是,在:冒號(hào)后面指定需要填充的內(nèi)容,可以使用上述4種格式化方式來(lái)對(duì)文本格式進(jìn)行控制,舉個(gè)例子
price = 23.1999
obj = 'bread'
print("The {}'s price is {:.2f}".format(obj, price)) # 使用默認(rèn)位置方式,保留兩位小數(shù)
print("The {0}'s price is {1:.2f}".format(obj, price)) # 使用指定位置方式,保留兩位小數(shù)
print("The {name}'s price is {price:.2f}".format(name=obj, price=price)) # 使用字典方式,保留兩位小數(shù)
li = [obj, price]
print("The {}'s price is {:.2f}".format(*li)) # 使用列表解包的方式,保留兩位小數(shù)
info = {'name':obj, 'price':price}
print("The {name}'s price is {price:.2f}".format(**info)) # 使用字典解包的方式,保留兩位小數(shù)
結(jié)果如下
The bread's price is 23.20
The bread's price is 23.20
The bread's price is 23.20
The bread's price is 23.20
The bread's price is 23.20
四、Reference
https://www.runoob.com/python/python-strings.html
到此這篇關(guān)于Python format字符串格式化函數(shù)的使用的文章就介紹到這了,更多相關(guān)Python format字符串格式化 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)python3.4 字符串轉(zhuǎn)16進(jìn)制的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python3.4 字符串轉(zhuǎn)16進(jìn)制的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
4種非常實(shí)用的python內(nèi)置數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了4種非常實(shí)用的python內(nèi)置數(shù)據(jù)結(jié)構(gòu),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
Python如何實(shí)現(xiàn)自帶HTTP文件傳輸服務(wù)
這篇文章主要介紹了Python如何實(shí)現(xiàn)自帶HTTP文件傳輸服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Python使用tkinter模塊實(shí)現(xiàn)推箱子游戲
這篇文章主要介紹了Python使用tkinter模塊實(shí)現(xiàn)推箱子游戲,主要分享兩點(diǎn),第一就是這個(gè)程序的實(shí)現(xiàn)過(guò)程,第二點(diǎn)就是我在編寫(xiě)過(guò)程中的一些思考。本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參看下吧2019-10-10
Python使用ffmpeg合成視頻、音頻的實(shí)現(xiàn)方法
這篇文章主要介紹了Python使用ffmpeg合成視頻、音頻,通過(guò)本文的學(xué)習(xí)能幫助大家了解如何在python中調(diào)用ffmpeg模塊,對(duì)此進(jìn)行音視頻合并,完成視頻合成,需要的朋友可以參考下2022-04-04
Python的numpy庫(kù)中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法
下面小編就為大家分享一篇Python的numpy庫(kù)中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Python從臨時(shí)郵箱獲取驗(yàn)證碼的操作代碼
這篇文章主要介紹了Python從臨時(shí)郵箱獲取驗(yàn)證碼的操作代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
python熱力圖實(shí)現(xiàn)的完整實(shí)例
熱力圖的使用場(chǎng)景有描述數(shù)據(jù)在空間的密集程度,常見(jiàn)有城市熱力圖、區(qū)域熱力圖,描述多個(gè)變量之間相關(guān)性高低程度,這篇文章主要給大家介紹了關(guān)于python熱力圖實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-06-06

