Python內建format函數詳細介紹

來源:互聯網
上載者:User
英文文檔:

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.

說明:

  1. 函數功能將一個數值進行格式化顯示。

  2. 如果參數format_spec未提供,則和調用str(value)效果相同,轉換成字串格式化。

>>> format(3.1415936)'3.1415936'>>> str(3.1415926)'3.1415926'

  3. 對於不同的類型,參數format_spec可提供的值都不一樣

#字串可以提供的參數 's' None>>> format('some string','s')'some string'>>> format('some string')'some string'#整形數值可以提供的參數有 'b' 'c' 'd' 'o' 'x' 'X' 'n' None>>> format(3,'b') #轉換成二進位'11'>>> format(97,'c') #轉換unicode成字元'a'>>> format(11,'d') #轉換成10進位'11'>>> format(11,'o') #轉換成8進位'13'>>> format(11,'x') #轉換成16進位 小寫字母表示'b'>>> format(11,'X') #轉換成16進位 大寫字母表示'B'>>> format(11,'n') #和d一樣'11'>>> format(11) #預設和d一樣'11'#浮點數可以提供的參數有 'e' 'E' 'f' 'F' 'g' 'G' 'n' '%' None>>> format(314159267,'e') #科學計數法,預設保留6位小數'3.141593e+08'>>> format(314159267,'0.2e') #科學計數法,指定保留2位小數'3.14e+08'>>> format(314159267,'0.2E') #科學計數法,指定保留2位小數,採用大寫E表示'3.14E+08'>>> format(314159267,'f') #小數點計數法,預設保留6位小數'314159267.000000'>>> format(3.14159267000,'f') #小數點計數法,預設保留6位小數'3.141593'>>> format(3.14159267000,'0.8f') #小數點計數法,指定保留8位小數'3.14159267'>>> format(3.14159267000,'0.10f') #小數點計數法,指定保留10位小數'3.1415926700'>>> format(3.14e+1000000,'F')  #小數點計數法,無窮大轉換成大小字母'INF'#g的格式化比較特殊,假設p為格式中指定的保留小數位元,先嘗試採用科學計數法格式化,得到冪指數exp,如果-4<=exp<p,則採用小數計數法,並保留p-1-exp位小數,否則按小數計數法計數,並按p-1保留小數位元>>> format(0.00003141566,'.1g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學計數法計數,保留0位小數點'3e-05'>>> format(0.00003141566,'.2g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學計數法計數,保留1位小數點'3.1e-05'>>> format(0.00003141566,'.3g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學計數法計數,保留2位小數點'3.14e-05'>>> format(0.00003141566,'.3G') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科學計數法計數,保留0位小數點,E使用大寫'3.14E-05'>>> format(3.1415926777,'.1g') #p=1,exp=0 ==》 -4<=exp<p成立,按小數計數法計數,保留0位小數點'3'>>> format(3.1415926777,'.2g') #p=1,exp=0 ==》 -4<=exp<p成立,按小數計數法計數,保留1位小數點'3.1'>>> format(3.1415926777,'.3g') #p=1,exp=0 ==》 -4<=exp<p成立,按小數計數法計數,保留2位小數點'3.14'>>> format(0.00003141566,'.1n') #和g相同'3e-05'>>> format(0.00003141566,'.3n') #和g相同'3.14e-05'>>> format(0.00003141566) #和g相同'3.141566e-05'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.