Python通過(guò)format函數(shù)格式化顯示值
英文文檔:
format(value[, format_spec])
Convert a value to a “formatted” representation, as controlled by format_spec. The interpretation of format_spec will depend on the type of the value argument, however there is a standard formatting syntax that is used by most built-in types: Format Specification Mini-Language.
The default format_spec is an empty string which usually gives the same effect as calling str(value).
A call to format(value, format_spec) is translated to type(value).__format__(value, format_spec) which bypasses the instance dictionary when searching for the value's __format__() method. A TypeError exception is raised if the method search reaches object and the format_spec is non-empty, or if either the format_spec or the return value are not strings.
格式化顯示值
說(shuō)明:
1. 函數(shù)功能將一個(gè)數(shù)值進(jìn)行格式化顯示。
2. 如果參數(shù)format_spec未提供,則和調(diào)用str(value)效果相同,轉(zhuǎn)換成字符串格式化。
>>> format(3.1415936) '3.1415936' >>> str(3.1415926) '3.1415926'
3. 對(duì)于不同的類型,參數(shù)format_spec可提供的值都不一樣
#字符串可以提供的參數(shù) 's' None >>> format('some string','s') 'some string' >>> format('some string') 'some string' #整形數(shù)值可以提供的參數(shù)有 'b' 'c' 'd' 'o' 'x' 'X' 'n' None >>> format(3,'b') #轉(zhuǎn)換成二進(jìn)制 '11' >>> format(97,'c') #轉(zhuǎn)換unicode成字符 'a' >>> format(11,'d') #轉(zhuǎn)換成10進(jìn)制 '11' >>> format(11,'o') #轉(zhuǎn)換成8進(jìn)制 '13' >>> format(11,'x') #轉(zhuǎn)換成16進(jìn)制 小寫字母表示 'b' >>> format(11,'X') #轉(zhuǎn)換成16進(jìn)制 大寫字母表示 'B' >>> format(11,'n') #和d一樣 '11' >>> format(11) #默認(rèn)和d一樣 '11' #浮點(diǎn)數(shù)可以提供的參數(shù)有 'e' 'E' 'f' 'F' 'g' 'G' 'n' '%' None >>> format(314159267,'e') #科學(xué)計(jì)數(shù)法,默認(rèn)保留6位小數(shù) '3.141593e+08' >>> format(314159267,'0.2e') #科學(xué)計(jì)數(shù)法,指定保留2位小數(shù) '3.14e+08' >>> format(314159267,'0.2E') #科學(xué)計(jì)數(shù)法,指定保留2位小數(shù),采用大寫E表示 '3.14E+08' >>> format(314159267,'f') #小數(shù)點(diǎn)計(jì)數(shù)法,默認(rèn)保留6位小數(shù) '314159267.000000' >>> format(3.14159267000,'f') #小數(shù)點(diǎn)計(jì)數(shù)法,默認(rèn)保留6位小數(shù) '3.141593' >>> format(3.14159267000,'0.8f') #小數(shù)點(diǎn)計(jì)數(shù)法,指定保留8位小數(shù) '3.14159267' >>> format(3.14159267000,'0.10f') #小數(shù)點(diǎn)計(jì)數(shù)法,指定保留10位小數(shù) '3.1415926700' >>> format(3.14e+1000000,'F') #小數(shù)點(diǎn)計(jì)數(shù)法,無(wú)窮大轉(zhuǎn)換成大小字母 'INF' #g的格式化比較特殊,假設(shè)p為格式中指定的保留小數(shù)位數(shù),先嘗試采用科學(xué)計(jì)數(shù)法格式化,得到冪指數(shù)exp,如果-4<=exp<p,則采用小數(shù)計(jì)數(shù)法,并保留p-1-exp位小數(shù),否則按小數(shù)計(jì)數(shù)法計(jì)數(shù),并按p-1保留小數(shù)位數(shù) >>> format(0.00003141566,'.1g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學(xué)計(jì)數(shù)法計(jì)數(shù),保留0位小數(shù)點(diǎn) '3e-05' >>> format(0.00003141566,'.2g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學(xué)計(jì)數(shù)法計(jì)數(shù),保留1位小數(shù)點(diǎn) '3.1e-05' >>> format(0.00003141566,'.3g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學(xué)計(jì)數(shù)法計(jì)數(shù),保留2位小數(shù)點(diǎn) '3.14e-05' >>> format(0.00003141566,'.3G') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學(xué)計(jì)數(shù)法計(jì)數(shù),保留0位小數(shù)點(diǎn),E使用大寫 '3.14E-05' >>> format(3.1415926777,'.1g') #p=1,exp=0 ==》 -4<=exp<p成立,按小數(shù)計(jì)數(shù)法計(jì)數(shù),保留0位小數(shù)點(diǎn) '3' >>> format(3.1415926777,'.2g') #p=1,exp=0 ==》 -4<=exp<p成立,按小數(shù)計(jì)數(shù)法計(jì)數(shù),保留1位小數(shù)點(diǎn) '3.1' >>> format(3.1415926777,'.3g') #p=1,exp=0 ==》 -4<=exp<p成立,按小數(shù)計(jì)數(shù)法計(jì)數(shù),保留2位小數(shù)點(diǎn) '3.14' >>> format(0.00003141566,'.1n') #和g相同 '3e-05' >>> format(0.00003141566,'.3n') #和g相同 '3.14e-05' >>> format(0.00003141566) #和g相同 '3.141566e-05'
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
tensorflow 獲取所有variable或tensor的name示例
今天小編就為大家分享一篇tensorflow 獲取所有variable或tensor的name示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python實(shí)現(xiàn)猜拳與猜數(shù)字游戲的方法詳解
本文將為大家介紹兩個(gè)用Python語(yǔ)言實(shí)現(xiàn)的小案例:猜拳游戲與數(shù)字猜猜猜小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04pyqt5 實(shí)現(xiàn)在別的窗口彈出進(jìn)度條
今天小編就為大家分享一篇pyqt5 實(shí)現(xiàn)在別的窗口彈出進(jìn)度條,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例
這篇文章主要介紹了基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Python讀取excel文件中帶公式的值的實(shí)現(xiàn)
這篇文章主要介紹了Python讀取excel文件中帶公式的值的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04