欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?format字符串格式化函數(shù)的使用

 更新時間:2022年01月14日 08:39:45   作者:木心  
本文主要介紹了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ò)爬蟲項目:內(nèi)容提取器的定義

    本篇文章主要介紹了Python網(wǎng)絡(luò)爬蟲項目,這能有效的節(jié)省程序員的時間,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • plt.subplot()參數(shù)及使用介紹

    plt.subplot()參數(shù)及使用介紹

    本文主要介紹了plt.subplot()參數(shù)及使用介紹,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • python基于tkinter制作m3u8視頻下載工具

    python基于tkinter制作m3u8視頻下載工具

    這篇文章主要介紹了python如何基于tkinter制作m3u8視頻下載工具,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • Python?imgaug庫安裝與使用教程(圖片加模糊光雨雪霧等特效)

    Python?imgaug庫安裝與使用教程(圖片加模糊光雨雪霧等特效)

    imgaug機器學(xué)習(xí)實驗中的圖像增強庫,特別是卷積神經(jīng)網(wǎng)絡(luò),支持以多種不同方式增強圖像、關(guān)鍵點/地標、邊界框、熱圖和分割圖,這篇文章主要介紹了Python?imgaug庫?安裝與使用教程(圖片加模糊光雨雪霧等特效),需要的朋友可以參考下
    2022-11-11
  • pyqt4教程之實現(xiàn)windows窗口小示例分享

    pyqt4教程之實現(xiàn)windows窗口小示例分享

    這篇文章主要介紹了pyqt4實現(xiàn)windows窗口小示例,需要的朋友可以參考下
    2014-03-03
  • Python實現(xiàn)類別變量的獨熱編碼

    Python實現(xiàn)類別變量的獨熱編碼

    這篇文章主要為大家詳細介紹了基于Python下OneHotEncoder與pd.get_dummies兩種方法,實現(xiàn)機器學(xué)習(xí)中最優(yōu)的編碼方法——獨熱編碼的方法,需要的可以參考一下
    2023-02-02
  • 詳解python eval函數(shù)的妙用

    詳解python eval函數(shù)的妙用

    這篇文章主要介紹了詳解python eval函數(shù)的妙用,詳細介紹了python eval函數(shù)的具體用法和實例,有興趣的可以了解一下
    2017-11-11
  • Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換

    Python操作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-06
  • python內(nèi)置模塊collections知識點總結(jié)

    python內(nèi)置模塊collections知識點總結(jié)

    這篇文章主要介紹了python內(nèi)置模塊collections知識點總結(jié),有興趣的朋友們學(xué)習(xí)下。
    2019-12-12
  • python使用pgzero進行游戲開發(fā)

    python使用pgzero進行游戲開發(fā)

    今天要和大家分享的pgzero(pygame zero)是在pygame基礎(chǔ)上做了進一步的封裝,使得設(shè)計一款游戲十分的方便,特別適合少兒編程領(lǐng)域的教學(xué), 與scratch相得益彰。
    2021-06-06

最新評論