原文:https://www.cnblogs.com/xxby/p/5571620.html
字符串格式化
Python的字符串格式化有兩種方式:%格式符方式苔可,format方式
%格式符
%[(name)][flags][width].[precision]typecode
(name) ? ? ?可選,用于選擇指定的key
flags ? ? ? ? ?可選袋狞,可供選擇的值有:
+ ? ? ? 右對(duì)齊焚辅;正數(shù)前加正好,負(fù)數(shù)前加負(fù)號(hào)苟鸯;
- ? ? ? ?左對(duì)齊同蜻;正數(shù)前無符號(hào),負(fù)數(shù)前加負(fù)號(hào)早处;
空格 ? ?右對(duì)齊湾蔓;正數(shù)前加空格,負(fù)數(shù)前加負(fù)號(hào)砌梆;
0 ? ? ? ?右對(duì)齊默责;正數(shù)前無符號(hào),負(fù)數(shù)前加負(fù)號(hào)么库;用0填充空白處
width ? ? ? ? 可選傻丝,占有寬度
.precision ? 可選,小數(shù)點(diǎn)后保留的位數(shù)
typecode ? ?必選
s诉儒,獲取傳入對(duì)象的__str__方法的返回值葡缰,并將其格式化到指定位置
r,獲取傳入對(duì)象的__repr__方法的返回值忱反,并將其格式化到指定位置
c泛释,整數(shù):將數(shù)字轉(zhuǎn)換成其unicode對(duì)應(yīng)的值,10進(jìn)制范圍為?0 <= i <= 1114111(py27則只支持0-255)温算;字符:將字符添加到指定位置
o怜校,將整數(shù)轉(zhuǎn)換成 八 ?進(jìn)制表示,并將其格式化到指定位置
x注竿,將整數(shù)轉(zhuǎn)換成十六進(jìn)制表示茄茁,并將其格式化到指定位置
d魂贬,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 十 進(jìn)制表示裙顽,并將其格式化到指定位置
e付燥,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法愈犹,并將其格式化到指定位置(小寫e)
E键科,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法漩怎,并將其格式化到指定位置(大寫E)
f勋颖, 將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成浮點(diǎn)數(shù)表示勋锤,并將其格式化到指定位置(默認(rèn)保留小數(shù)點(diǎn)后6位)
F饭玲,同上
g,自動(dòng)調(diào)整將整數(shù)怪得、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示(超過6位數(shù)用科學(xué)計(jì)數(shù)法)咱枉,并將其格式化到指定位置(如果是科學(xué)計(jì)數(shù)則是e卑硫;)
G徒恋,自動(dòng)調(diào)整將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示(超過6位數(shù)用科學(xué)計(jì)數(shù)法)欢伏,并將其格式化到指定位置(如果是科學(xué)計(jì)數(shù)則是E入挣;)
%,當(dāng)字符串中存在格式化標(biāo)志時(shí)硝拧,需要用 %%表示一個(gè)百分號(hào)
注:Python中百分號(hào)格式化是不存在自動(dòng)將整數(shù)轉(zhuǎn)換成二進(jìn)制表示的方式
(name) ? ? ?可選径筏,用于選擇指定的key
a ="%(name)s-----%(age)d "%{'name':'xx','age':20}print(a)
執(zhí)行結(jié)果:
xx-----20
flags ? ? ? ? ?可選,可供選擇的值有:
+ ? ? ? 右對(duì)齊障陶;正數(shù)前加正好滋恬,負(fù)數(shù)前加負(fù)號(hào)刻肄;
- ? ? ? ?左對(duì)齊彤悔;正數(shù)前無符號(hào)幌衣,負(fù)數(shù)前加負(fù)號(hào)艳馒;
空格 ? ?右對(duì)齊拟赊;正數(shù)前加空格配并,負(fù)數(shù)前加負(fù)號(hào)贤壁;
0 ? ? ? ?右對(duì)齊苔货;正數(shù)前無符號(hào)妈候,負(fù)數(shù)前加負(fù)號(hào)敢靡;用0填充空白處
width ? ? ? ? 可選,占有寬度
name占10位苦银,+,右對(duì)齊啸胧,age占10位赶站,-,左對(duì)齊
b ="%(name)+10s————————%(age)-10d————————"%{'name':'xx','age':20}print(b)
執(zhí)行結(jié)果:
? ? ? ? xx————————20? ? ? ? ————————
?空格纺念,右對(duì)齊
?0亲怠,用0填充空白處
c ="------%(year) d******%(age)010d "%{'year':2016,'age':-20}print(c)
執(zhí)行結(jié)果:
------ 2016******-000000020
.precision ? 可選,小數(shù)點(diǎn)后保留的位數(shù)
只保留兩位小數(shù)
d ='--------%(p).2f'%{'p':1.23456}
d1 ='--------%(p)f'%{'p':1.23456}print(d)print(d1)
執(zhí)行結(jié)果:
--------1.23
--------1.234560
typecode ? ?必選
c柠辞,整數(shù):將數(shù)字轉(zhuǎn)換成其unicode對(duì)應(yīng)的值团秽,10進(jìn)制范圍為?0 <= i <= 1114111(py27則只支持0-255);字符:將字符添加到指定位置
o叭首,將整數(shù)轉(zhuǎn)換成 八 ?進(jìn)制表示习勤,并將其格式化到指定位置
x,將整數(shù)轉(zhuǎn)換成十六進(jìn)制表示焙格,并將其格式化到指定位置
e ='***%c***%o***%x'%(65,15,15)print(e)
執(zhí)行結(jié)果:
***A***17***f
e图毕,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法眷唉,并將其格式化到指定位置(小寫e)
E予颤,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法冬阳,并將其格式化到指定位置(大寫E)
f ='-----%(num)e------%(num)E'%{'num':1000000000}print(f)
執(zhí)行結(jié)果:
-----1.000000e+09------1.000000E+09
g蛤虐,自動(dòng)調(diào)整將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示(超過6位數(shù)用科學(xué)計(jì)數(shù)法)肝陪,并將其格式化到指定位置(如果是科學(xué)計(jì)數(shù)則是e驳庭;)
G,自動(dòng)調(diào)整將整數(shù)氯窍、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示(超過6位數(shù)用科學(xué)計(jì)數(shù)法)饲常,并將其格式化到指定位置(如果是科學(xué)計(jì)數(shù)則是E;)
g ='-----%(num)g------%(num1)G'%{'num':1000000000,'num1':100}print(g)
執(zhí)行結(jié)果:
-----1e+09------100
%狼讨,當(dāng)字符串中存在格式化標(biāo)志時(shí)贝淤,需要用 %%表示一個(gè)百分號(hào)(類似于轉(zhuǎn)意效果)
s ='aaa %'print(s)
s1 ='aaa %s %%'%('bbb')print(s1)
執(zhí)行結(jié)果:
aaa %aaa bbb %
format方式
[[fill]align][sign][#][0][width][,][.precision][type]
fill ? ? ? ? ? 【可選】空白處填充的字符
align ? ? ? ?【可選】對(duì)齊方式(需配合width使用)
<,內(nèi)容左對(duì)齊
>政供,內(nèi)容右對(duì)齊(默認(rèn))
=播聪,內(nèi)容右對(duì)齊,將符號(hào)放置在填充字符的左側(cè)鲫骗,且只對(duì)數(shù)字類型有效犬耻。 即使:符號(hào)+填充物+數(shù)字
^,內(nèi)容居中
sign ? ? ? ? 【可選】有無符號(hào)數(shù)字
+执泰,正號(hào)加正枕磁,負(fù)號(hào)加負(fù);
?-术吝,正號(hào)不變计济,負(fù)號(hào)加負(fù)茸苇;
空格 ,正號(hào)空格沦寂,負(fù)號(hào)加負(fù)学密;
# ? ? ? ? ? ?【可選】對(duì)于二進(jìn)制、八進(jìn)制传藏、十六進(jìn)制腻暮,如果加上#,會(huì)顯示 0b/0o/0x毯侦,否則不顯示
哭靖, ? ? ? ? ? ?【可選】為數(shù)字添加分隔符,如:1,000,000
width ? ? ? 【可選】格式化位所占寬度
.precision 【可選】小數(shù)位保留精度
type ? ? ? ? 【可選】格式化類型
傳入” 字符串類型 “的參數(shù)
s侈离,格式化字符串類型數(shù)據(jù)
空白试幽,未指定類型,則默認(rèn)是None卦碾,同s
傳入“ 整數(shù)類型 ”的參數(shù)
b铺坞,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成2進(jìn)制表示然后格式化
c,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換為其對(duì)應(yīng)的unicode字符
d洲胖,十進(jìn)制整數(shù)
o济榨,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成8進(jìn)制表示然后格式化;
x宾濒,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成16進(jìn)制表示然后格式化(小寫x)
X腿短,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成16進(jìn)制表示然后格式化(大寫X)
傳入“ 浮點(diǎn)型或小數(shù)類型?”的參數(shù)
e, 轉(zhuǎn)換為科學(xué)計(jì)數(shù)法(小寫e)表示绘梦,然后格式化;
E赴魁, 轉(zhuǎn)換為科學(xué)計(jì)數(shù)法(大寫E)表示卸奉,然后格式化;
f , 轉(zhuǎn)換為浮點(diǎn)型(默認(rèn)小數(shù)點(diǎn)后保留6位)表示颖御,然后格式化榄棵;
F, 轉(zhuǎn)換為浮點(diǎn)型(默認(rèn)小數(shù)點(diǎn)后保留6位)表示潘拱,然后格式化疹鳄;
g, 自動(dòng)在e和f中切換
G芦岂, 自動(dòng)在E和F中切換
%瘪弓,顯示百分比(默認(rèn)顯示小數(shù)點(diǎn)后6位)
fill ? ? ? ? ? 【可選】空白處填充的字符
align ? ? ? ?【可選】對(duì)齊方式(需配合width使用)
<,內(nèi)容左對(duì)齊
>禽最,內(nèi)容右對(duì)齊(默認(rèn))
=腺怯,內(nèi)容右對(duì)齊袱饭,將符號(hào)放置在填充字符的左側(cè),且只對(duì)數(shù)字類型有效呛占。 即使:符號(hào)+填充物+數(shù)字
^虑乖,內(nèi)容居中
width ? ? ? 【可選】格式化位所占寬度
s1 ='---{:*^20s}----'.format('welcome')print(s1)
s2 ='---{:*>20s}----'.format('welcome')print(s2)
s3 ='---{:*<20s}----'.format('welcome')print(s3)
執(zhí)行結(jié)果:
---******welcome*******----
---*************welcome----
---welcome*************----
# ? ? ? ? ? ?【可選】對(duì)于二進(jìn)制、八進(jìn)制晾虑、十六進(jìn)制疹味,如果加上#,會(huì)顯示 0b/0o/0x帜篇,否則不顯示
b佛猛,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成2進(jìn)制表示然后格式化
c,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換為其對(duì)應(yīng)的unicode字符
d坠狡,十進(jìn)制整數(shù)
o继找,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成8進(jìn)制表示然后格式化;
x逃沿,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成16進(jìn)制表示然后格式化(小寫x)
X婴渡,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成16進(jìn)制表示然后格式化(大寫X)
三種方法表示
a1 ="numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{:c}".format(15, 15, 15, 15, 15, 15.87623,65)
a2 ="numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%},{1:c}".format(15,65)
a3 ="numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%},{cc:c}".format(num=15,cc=65)print(a1)print(a2)print(a3)
執(zhí)行結(jié)果:
numbers: 1111,17,15,f,F, 1587.623000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
, ? ? ? ? ? ?【可選】為數(shù)字添加分隔符凯亮,如:1,000,000
.precision 【可選】小數(shù)位保留精度
n ='---{:,d}----'.format(10000000)
n1 ='---{:.2f}----'.format(1.2345)print(n)print(n1)
執(zhí)行結(jié)果:
---10,000,000----
---1.23----
?format常用格式化
tp1 ="i am {}, age {}, {}".format("seven", 18,'alex')
tp2 ="i am {}, age {}, {}".format(*["seven", 18,'alex'])
tp3 ="i am {0}, age {1}, really {0}".format("seven", 18)
tp4 ="i am {0}, age {1}, really {0}".format(*["seven", 18])
tp5 ="i am {name}, age {age}, really {name}".format(name="seven", age=18)
tp6 ="i am {name}, age {age}, really {name}".format(**{"name":"seven","age": 18})
tp7 ="i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tp8 ="i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
tp9 ="i am {:s}, age {:d}".format(*["seven", 18])
tp10 ="i am {name:s}, age {age:d}".format(name="seven", age=18)
tp11 ="i am {name:s}, age {age:d}".format(**{"name":"seven","age": 18})print(tp1)print(tp2)print(tp3)print(tp4)print(tp5)print(tp6)print(tp7)print(tp8)print(tp9)print(tp10)print(tp11)
執(zhí)行結(jié)果:
i am seven, age 18, alex
i am seven, age 18, alex
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am 1, age 2, really 3i am seven, age 18, money 88888.100000i am seven, age 18i am seven, age 18i am seven, age 18