Python 程序設(shè)計(jì)入門(018)—— format() 函數(shù)的用法詳解
一、format() 函數(shù)的基本格式
format() 函數(shù)可以對(duì)數(shù)據(jù)進(jìn)行格式化處理,將值轉(zhuǎn)換為由 format_spec 控制的【格式化】表示形式。format() 函數(shù)的語(yǔ)法格式如下:
format(value, format_spec)
說(shuō)明:
(1)value:要轉(zhuǎn)換的數(shù)據(jù)。
(2)format_spec:格式化解釋,取決于值參數(shù)的類型;
(3)默認(rèn)情況下,format_spec 是一個(gè)空字符串,通常與調(diào)用 str(value) 函數(shù)具有相同的效果。
(4)format_spec 參數(shù)的格式包括填充值、文字對(duì)齊、標(biāo)志設(shè)置、格式化、類型轉(zhuǎn)換、千位符等應(yīng)用。格式如下:
format_spec ::=[fill[align]] [sign] [z] [#] [0] [width] [grouping_option] [.precision] [type]
其中:
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= digit+
grouping_option ::= "_" | ","
precision ::= digit+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X"
二、不提供 format_spec 參數(shù)
如果不提供 format_spec 參數(shù),默認(rèn)將其他格式數(shù)據(jù)轉(zhuǎn)換為字符串類型,和調(diào)用 str(value) 函數(shù)的效果相同。
例如:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-776271.html
print("圓周率:" + format(3.14)) # 將浮點(diǎn)數(shù)轉(zhuǎn)換為字符串
print("圓周率:" + str(3.14))
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
圓周率:3.14
圓周率:3.14
三、設(shè)置字符串的對(duì)齊方式(align)
使用如下選項(xiàng)設(shè)置字符串的對(duì)齊方式:
符號(hào) | 含義 |
---|---|
‘<’ | 強(qiáng)制字段在有效空間內(nèi)保持左對(duì)齊(這是大多數(shù)對(duì)象的默認(rèn)設(shè)置) |
‘>’ | 強(qiáng)制字段在有效空間內(nèi)保持右對(duì)齊(這是大多數(shù)對(duì)象的默認(rèn)設(shè)置) |
‘=’ | 強(qiáng)制將填充字符放置在符號(hào)(如果有)之后、數(shù)字之前,例如:+000000120。這種對(duì)齊選項(xiàng)僅對(duì)數(shù)字類型有效。當(dāng)【0】緊接在字符寬度之前時(shí),它將成為填充數(shù)字時(shí)的默認(rèn)值。 |
‘^’ | 強(qiáng)制字段在有效空間內(nèi)保持居中對(duì)齊 |
例如:
print("圓周率:" + format(3.14,"10.2f") + "|end") # 默認(rèn)為右對(duì)齊
print("圓周率:" + format(3.14,">10.2f") + "|end") # 設(shè)置為右對(duì)齊
print("圓周率:" + format(3.14,"<10.2f") + "|end") # 設(shè)置為左對(duì)齊
print("圓周率:" + format(3.14,"^10.2f") + "|end") # 設(shè)置為居中對(duì)齊
print("圓周率:" + format(3.14,"A>10.2f") + "|end") # 設(shè)置為右對(duì)齊,前面填充A
print("圓周率:" + format(-3.14,"A>10.2f") + "|end") # 設(shè)置為右對(duì)齊,前面填充A(A位于-符號(hào)之前)
print("圓周率:" + format(-3.14,"A=10.2f") + "|end") # 數(shù)字前面填充A(A位于-符號(hào)之后)
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
圓周率: 3.14|end
圓周率: 3.14|end
圓周率:3.14 |end
圓周率: 3.14 |end
圓周率:AAAAAA3.14|end
圓周率:AAAAA-3.14|end
圓周率:-AAAAA3.14|end
四、設(shè)置 sign 選項(xiàng)
sign 選項(xiàng)只對(duì)數(shù)字類型有效,sign 選項(xiàng)的含義如下:
符號(hào) | 含義 |
---|---|
‘+’ | 表示正負(fù)數(shù)均可使用符號(hào) |
‘-’ | 指示符號(hào)只被用于負(fù)數(shù)(這是默認(rèn)行為) |
’ ’ | 表示正數(shù)應(yīng)使用前導(dǎo)空格,負(fù)數(shù)使用負(fù)號(hào) |
例如:
print("圓周率:" + format(3.14,"+") + "|end") # 正數(shù)前面加+號(hào)
print("圓周率:" + format(-3.14,"+") + "|end") # 負(fù)數(shù)前面加-號(hào)
print("圓周率:" + format(3.14,"-") + "|end") # 正數(shù)前面不加+號(hào)
print("圓周率:" + format(-3.14,"-") + "|end") # 負(fù)數(shù)前面加+號(hào)
print("圓周率:" + format(3.14," ") + "|end") # 正數(shù)前面加空格
print("圓周率:" + format(-3.14," ") + "|end") # 負(fù)數(shù)前面加-號(hào)
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
圓周率:+3.14|end
圓周率:-3.14|end
圓周率:3.14|end
圓周率:-3.14|end
圓周率: 3.14|end
圓周率:-3.14|end
四、設(shè)置 # 選項(xiàng)
# 選項(xiàng)僅對(duì)整型、浮點(diǎn)型和負(fù)數(shù)型有效。# 選項(xiàng)的含義如下:
符號(hào) | 含義 |
---|---|
# | # 選項(xiàng)僅對(duì)整型、浮點(diǎn)型和負(fù)數(shù)型有效。 對(duì)于整數(shù),當(dāng)使用二進(jìn)制、八進(jìn)制或十六進(jìn)制輸出時(shí),該選項(xiàng)會(huì)在輸出值中添加相應(yīng)的前綴:0b、0o、0x 或0X。 對(duì)于浮點(diǎn)型和負(fù)數(shù)型,即使后面沒有數(shù)字,替換形式會(huì)導(dǎo)致轉(zhuǎn)換結(jié)果始終包含小數(shù)點(diǎn)字符。 對(duì)于 g 和 G 的科學(xué)計(jì)數(shù)法小數(shù)點(diǎn)的轉(zhuǎn)換,不會(huì)從結(jié)果中刪除尾隨零。 |
例如:
print("以下為整型數(shù)據(jù)的輸出:")
print(format(11) + "|end") # 輸出結(jié)果為十進(jìn)制
print(format(11,"#") + "|end") # 輸出結(jié)果為十進(jìn)制
print(format(11,"b") + "|end") # 輸出結(jié)果為二進(jìn)制
print(format(11,"#b") + "|end") # 輸出結(jié)果為二進(jìn)制(輸入0b)
print(format(11,"o") + "|end") # 輸出結(jié)果為八進(jìn)制
print(format(11,"#o") + "|end") # 輸出結(jié)果為八進(jìn)制(輸入0o)
print(format(11,"x") + "|end") # 輸出結(jié)果為十六進(jìn)制
print(format(11,"#x") + "|end") # 輸出結(jié)果為十六進(jìn)制(輸入0x)
print(format(11,"X") + "|end") # 輸出結(jié)果為十六進(jìn)制
print(format(11,"#X") + "|end") # 輸出結(jié)果為十六進(jìn)制(輸入0X)
print("以下為浮點(diǎn)型數(shù)據(jù)的輸出:")
print(format(11.8,"10.0f") + "|end") # 無(wú)小數(shù)點(diǎn)
print(format(11.8,"#10.0f") + "|end") # 有小數(shù)點(diǎn)
print(format(-11.8,"#10.0F") + "|end")
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
以下為整型數(shù)據(jù)的輸出:
11|end
11|end
1011|end
0b1011|end
13|end
0o13|end
b|end
0xb|end
B|end
0XB|end
以下為浮點(diǎn)型數(shù)據(jù)的輸出:
12|end
12.|end
-12.|end
五、設(shè)置 grouping_option 選項(xiàng)
grouping_option 選項(xiàng)的含義如下:
符號(hào) | 含義 |
---|---|
, | 表示使用逗號(hào)作為千位分隔符。 |
_ | 對(duì)浮點(diǎn)型和整型使用下劃線作為千位分隔符。 對(duì)于整型 b、o、x 和 x,將每4位插入下劃線。 |
例如:
print(format(12345678) + "|end") # 不使用千位分隔符
print(format(12345678,",") + "|end") # 使用逗號(hào)作為千位分隔符
print(format(12345678.2541,",") + "|end") # 使用逗號(hào)作為千位分隔符
print(format(12345678.2541,"_") + "|end") # 使用下劃線作為千位分隔符
print(format(12345678,"_b") + "|end") # 使用逗號(hào)作為千位分隔符
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
12345678|end
12,345,678|end
12,345,678.2541|end
12_345_678.2541|end
1011_1100_0110_0001_0100_1110|end
六、設(shè)置 【0】和 width 選項(xiàng)
[0] 和 width 選項(xiàng)的含義如下:
符號(hào) | 含義 |
---|---|
[0] width |
width:定義字段總寬度,包括任何前綴、分隔符和其他格式字符。如果未指定,則字段寬度將由輸出內(nèi)容決定。 如果未指定對(duì)齊方式,則在寬度字段前面加一個(gè)零(0),相當(dāng)于對(duì)齊類型 sign 為 = 的填充字符 0。 |
例如:
print(format(3.14) + "|end") # 不指定寬度
print(format(3.14,"10") + "|end") # 指定寬度為10,默認(rèn)右對(duì)齊
print(format(3.14,"<10") + "|end") # 指定寬度為10,設(shè)置左對(duì)齊
print(format(3.14,"010") + "|end") # 指定寬度為10,左邊用0填充
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
3.14|end
3.14|end
3.14 |end
0000003.14|end
七、設(shè)置 【.precision】選項(xiàng)
[.precision] 選項(xiàng)的含義如下:
符號(hào) | 含義 |
---|---|
[.precision] | 設(shè)置浮點(diǎn)數(shù)的精度,是一個(gè)十進(jìn)制整數(shù)。 表示浮點(diǎn)類型 f 的小數(shù)點(diǎn)之后應(yīng)顯示多少位數(shù)字。 表示科學(xué)計(jì)數(shù)法小數(shù)類型 g 的小數(shù)點(diǎn)前和后應(yīng)顯示多少位數(shù)。 |
例如:
print(format(3.1415926) + "|end") # 不指定寬度
print(format(3.1415926,"10.1f") + "|end") # 指定寬度為10,小數(shù)位數(shù)1
print(format(3.1415926,"10.2f") + "|end") # 指定寬度為10,小數(shù)位數(shù)2
print(format(3.1415926,".1f") + "|end") # 不指定寬度,小數(shù)位數(shù)1
print(format(3.1415926,".2f") + "|end") # 不指定寬度,小數(shù)位數(shù)2
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
3.1415926|end
3.1|end
3.14|end
3.1|end
3.14|end
八、設(shè)置 type 選項(xiàng)
type 選項(xiàng)針對(duì)不同的數(shù)據(jù)類型有如下格式:
1、字符串類型
type 選項(xiàng)及含義如下:
符號(hào) | 含義 |
---|---|
s | 字符串格式。 |
None | 和 s 一樣。 |
例如:
print(format("Chinese","0<10") + "|end") # 左對(duì)齊,右側(cè)用0填充
print(format("Chinese","0>10") + "|end") # 右對(duì)齊,左側(cè)用0填充
print(format("Chinese","0^10") + "|end") # 居中對(duì)齊,左右兩側(cè)用0填充
print(format("Chinese","<10.3") + "|end") # 左對(duì)齊,截取左端3個(gè)字符
print(format("Chinese",">10.3") + "|end") # 右對(duì)齊,截取左端3個(gè)字符
print(format("Chinese","^10.3") + "|end") # 居中對(duì)齊,截取左端3個(gè)字符
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
Chinese000|end
000Chinese|end
0Chinese00|end
Chi |end
Chi|end
Chi |end
2、整數(shù)類型
type 選項(xiàng)及含義如下:
符號(hào) | 含義 |
---|---|
b | 二進(jìn)制格式 |
c | 將字符在打印之前將整數(shù)轉(zhuǎn)換為相應(yīng)的 unicode 字符 |
d | 十進(jìn)制整數(shù) |
o | 八進(jìn)制格式 |
x | 十六進(jìn)制格式,使用小寫字母表示 9 以上的數(shù)碼 |
X | 十六進(jìn)制格式,使用大寫字母表示 9 以上的數(shù)碼 |
n | 與 d 相似,不同之處在于它會(huì)使用當(dāng)前區(qū)域設(shè)置來(lái)插入適當(dāng)?shù)臄?shù)字分隔字符 |
例如:
print(format(1234567,"b") + "|end") # 二進(jìn)制
print(format(1234567,"o") + "|end") # 八進(jìn)制
print(format(1234567,"d") + "|end") # 十進(jìn)制
print(format(1234567,"n") + "|end") # 十進(jìn)制
print(format(1234567,"x") + "|end") # 十六進(jìn)制
print(format(1234567,"X") + "|end") # 十六進(jìn)制
print(format(4567,"c") + "|end") # 打印Unicode字符
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
100101101011010000111|end
4553207|end
1234567|end
1234567|end
12d687|end
12D687|end
?|end
3、浮點(diǎn)型
type 選項(xiàng)及含義如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-776271.html
符號(hào) | 含義 |
---|---|
e | 科學(xué)計(jì)數(shù)法。 對(duì)于一個(gè)給定的精度 p,將數(shù)字格式化為以字母 e 分隔系數(shù)和指數(shù)的科學(xué)計(jì)數(shù)法形式。 系數(shù)在小數(shù)點(diǎn)之前有一位,之后有 p 位,總計(jì) p + 1 個(gè)有效數(shù)位。 如未指定精度,則會(huì)對(duì) float 采用小數(shù)點(diǎn)之后 6 位精度,而對(duì) Decimal 則顯示所有系數(shù)位。 如果小數(shù)點(diǎn)之后沒有數(shù)位,則小數(shù)點(diǎn)也會(huì)被略去,除非使用了 # 選項(xiàng)。 |
E | 科學(xué)計(jì)數(shù)法。 與 e 相似,不同之處在于它使用大寫字母 E 作為分隔字符。 |
f | 定點(diǎn)表示法。 對(duì)于一個(gè)給定的精度 p ,將數(shù)字格式化為在小數(shù)點(diǎn)之后恰好有 p 位的小數(shù)形式。 如未指定精度,則會(huì)對(duì) float 采用小數(shù)點(diǎn)之后 6 位精度,而對(duì) Decimal 則使用大到足夠顯示所有系數(shù)位的精度。如果小數(shù)點(diǎn)之后沒有數(shù)位,則小數(shù)點(diǎn)也會(huì)被略去,除非使用了 # 選項(xiàng)。 |
g | (1)常規(guī)格式。 對(duì)于給定精度 p >= 1,這會(huì)將數(shù)值舍入到 p 個(gè)有效數(shù)位,再將結(jié)果以定點(diǎn)表示法或科學(xué)計(jì)數(shù)法進(jìn)行格式化,具體取決于其值的大小。 精度 0 會(huì)被視為等價(jià)于精度1。 (2)如未指定精度,會(huì)對(duì) float 采用 6 個(gè)有效數(shù)位的精度。 對(duì)于 Decimal,結(jié)果的系數(shù)會(huì)沿用原值的系數(shù)數(shù)位;對(duì)于絕對(duì)值小于 1e-6 的值以及最小有效數(shù)位的位值大于 1 的數(shù)值將會(huì)使用科學(xué)計(jì)數(shù)法,在其他情況下則會(huì)使用定點(diǎn)表示法即科學(xué)計(jì)數(shù)法。 |
% | 百分比。 將數(shù)字乘以 100 并顯示為定點(diǎn)(f)格式,后面帶一個(gè)百分號(hào)。 |
例如:
print("以下為科學(xué)計(jì)數(shù)法表示:")
print(format(1234567.123,"e") + "|end") # 6位小數(shù)
print(format(1234567.123,"15.10e") + "|end")# 10位小數(shù)
print(format(1234567.123,"15.10E") + "|end")# 10位小數(shù),字母E大寫
print(format(1234567.123,"15.0e") + "|end") # 0位小數(shù)
print(format(1234567.123,"#15.0e") + "|end")# 0位小數(shù)
print("以下為定點(diǎn)數(shù)表示:")
print(format(1234567.123,"#15.10f") + "|end")# 10位小數(shù)
print(format(1234567.123,"15.0f") + "|end") # 0位小數(shù)
print(format(1234567.123,"#15.0f") + "|end") # 0位小數(shù)
print(format(0.123,".2%") + "|end") # 百分比
運(yùn)算結(jié)果為:
===================== RESTART: C:\Python\Python38\First.py =====================
以下為科學(xué)計(jì)數(shù)法表示:
1.234567e+06|end
1.2345671230e+06|end
1.2345671230E+06|end
1e+06|end
1.e+06|end
以下為定點(diǎn)數(shù)表示:
1234567.1229999999|end
1234567|end
1234567.|end
12.30%|end
到了這里,關(guān)于Python 程序設(shè)計(jì)入門(018)—— format() 函數(shù)的用法詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!