Python?format字符串格式化函數(shù)的使用
一、簡介
從Python2.6開始,新增了str.format()
,它增強了字符串格式化的功能。基本語法是通過 {}
和 :
來代替以前的 %
占位符。
二、占位符%方式
字符串格式符號用法如下
舉個例子:
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
來補零,和控制小數(shù)位數(shù)的.
舉個例子:
price = 23.1999 obj = 'milk' print("The %s's price is %03f" %(obj, price)) # 前面補三個零 print("The %s's price is %3.0f" %(obj, price)) # 最小總占位長度為3,控制輸出0個小數(shù) print("The %s's price is %3.3f" %(obj, price)) # 最小總占位長度為3,控制輸出3個小數(shù) print("The %s's price is %5.4f" %(obj, price)) # 最小總占位長度為5,控制輸出4個小數(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、使用默認位置方式
格式:string{}.format(x1, x2)
舉個例子
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)
舉個例子
price = 23.1999 obj = 'milk' print("The {0}'s price is {1}".format(obj, price))
結(jié)果如下
The milk's price is 23.1999
3、使用列表方式
其實這種方式就相當(dāng)于前兩種使用默認位置和使用指定位置的方式,只不過這里需要使用*
對列表進行解包,舉個例子
price = 23.1999 obj = 'milk' info = [obj, price] print("The {}'s price is {}".format(*info)) # 對info進行解包
結(jié)果如下
The milk's price is 23.1999
4、使用字典的鍵值對方式
格式:string(key).format(key=value)
舉個例子,當(dāng)然也可以用**
對字典進行解包
price = 23.1999 obj = 'milk' print("The {name}'s price is {pri}".format(name=obj, pri=price)) # 更進一步,對字典進行解包 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ù)字格式化的方式
需要注意的是,在:
冒號后面指定需要填充的內(nèi)容,可以使用上述4種格式化方式來對文本格式進行控制,舉個例子
price = 23.1999 obj = 'bread' print("The {}'s price is {:.2f}".format(obj, price)) # 使用默認位置方式,保留兩位小數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python網(wǎng)絡(luò)爬蟲項目:內(nèi)容提取器的定義
本篇文章主要介紹了Python網(wǎng)絡(luò)爬蟲項目,這能有效的節(jié)省程序員的時間,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-10-10Python?imgaug庫安裝與使用教程(圖片加模糊光雨雪霧等特效)
imgaug機器學(xué)習(xí)實驗中的圖像增強庫,特別是卷積神經(jīng)網(wǎng)絡(luò),支持以多種不同方式增強圖像、關(guān)鍵點/地標、邊界框、熱圖和分割圖,這篇文章主要介紹了Python?imgaug庫?安裝與使用教程(圖片加模糊光雨雪霧等特效),需要的朋友可以參考下2022-11-11pyqt4教程之實現(xiàn)windows窗口小示例分享
這篇文章主要介紹了pyqt4實現(xiàn)windows窗口小示例,需要的朋友可以參考下2014-03-03Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換
這篇文章主要介紹了Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換,JSON的全稱是 JavaScript Object Notation,是一種輕量級的數(shù)據(jù)交換格式,關(guān)于JSON的更多相關(guān)內(nèi)容感興趣的小伙伴可以參考一下2022-06-06python內(nèi)置模塊collections知識點總結(jié)
這篇文章主要介紹了python內(nèi)置模塊collections知識點總結(jié),有興趣的朋友們學(xué)習(xí)下。2019-12-12