pythonformat方法的格式控制練習(xí) python如何輸出百分?jǐn)?shù)?
python如何輸出百分?jǐn)?shù)?輸出百分比的兩種注:在python3環(huán)境下測(cè)試出來(lái)。:然后可以使用參數(shù)磁盤(pán)格式化:{:.2%}{:.2%}:總是顯示小數(shù)點(diǎn)后2位總是顯示小數(shù)點(diǎn)后2位:gtgtgtprint
python如何輸出百分?jǐn)?shù)?
輸出百分比的兩種
注:在python3環(huán)境下測(cè)試出來(lái)。
:然后可以使用參數(shù)磁盤(pán)格式化:{:.2%}
{:.2%}:總是顯示小數(shù)點(diǎn)后2位
總是顯示小數(shù)點(diǎn)后2位:
gtgtgtprint(#39percent:{:.2%}#(42/50))
percent:84.00%
不總是顯示小數(shù)位:{:.0%},即,將2轉(zhuǎn)成0:
gtgtgtprint(#39percent:{:.0%}#(42/50))
percent:84%
:格式化磁盤(pán)為float,接著去處理成%格式:{:.2f}%
與的區(qū)別是:
(1)必須對(duì)42/50乘以2100。
(2)的%在{}外邊,的%在{}里邊。
沒(méi)顯示小數(shù)點(diǎn)后2位:
gtgtgtprint(#39percent:{:.2f}%#(42/50*100))
percent:84.00%
沒(méi)顯示小數(shù)點(diǎn)后1位:
gtgtgtprint(#39percent:{:.1f}%#(42/50*100))
percent:84.0%
只沒(méi)顯示整數(shù)位:
gtgtgtprint(#39percent:{:.0f}%#(42/50*100))
percent:84%
那說(shuō)明
{}的意思是對(duì)應(yīng)format()的一個(gè)參數(shù),按默認(rèn)順序隨機(jī),參數(shù)序號(hào)從0開(kāi)始,{0}對(duì)應(yīng)format()的第一個(gè)參數(shù),{1}填寫(xiě)第二個(gè)參數(shù)。比如:
默認(rèn)順序:
gtgtgtprint(#39percent1:{:.2%},percent2:{:.1%}#(42/50,42/100))
percent1:84.00%,percent2:42.0%
委托順序:
{1:.1%}按第2個(gè)參數(shù);{0:.1%}隨機(jī)第1個(gè)參數(shù)。
gtgtgtprint(#39percent2:{1:.1%},percent1:{0:.1%}#(42/50,42/100))
percent2:42.0%,percent1:84.0%
python格式化原理?
那個(gè)模塊?字符串的format那就標(biāo)準(zhǔn)輸出的format,標(biāo)準(zhǔn)輸出的format都差不多跟c的printf一般,字符串的也是占位符替換作用那就是使輸出來(lái)也可以生成的字符串超好看啊