English Documents:
format
(value[, format_spec])
Convert a value to a "formatted" representation, as controlled by Format_spec. The interpretation of Format_spec would depend on the type of the value argument, however there is a Standard formatting syntax, that's used by the 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 be format(value, format_spec)
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 are non-empty, or if either th E Format_spec or the return value is not strings.
Description
1. function function to format a numeric value to display.
2. If the parameter format_spec is not provided, the same effect as the call to STR (value) is converted to a string format.
>>> format (3.1415936) ' 3.1415936 ' >>> str (3.1415926) ' 3.1415926 '
3. For different types, the parameter Format_spec can provide the same value
#字符串可以提供的参数 ' s ' none>>> format (' Some string ', ' s ') ' some string ' >>> format (' some string ') ' some string ' #整形数值可以提供的参数有 ' B ' C ' d ' o ' x ' x ' n ' none>>> format (3, ' B ') #转换成二进制 ' one ' >>> format (' C ') #转换unicode Cheng ' a ' >>> format (one, ' d ') #转换成10进制 ' one ' >>> format (' o ') #转换成8进制 ' + ' >>> format (one, ' X ') # Converted to 16 lowercase letters for ' B ' >>> format (one, ' X ') #转换成16进制 uppercase letters ' B ' >>> format (one, ' n ') #和d一样 ' one ' >>> Format (one) #默认和d一样 ' one-by-one ' #浮点数可以提供的参数有 ' e ' e ' ' f ' ' F ' ' n ' '% ' none>>> format (314159267, ' e ') #科学计数法, reserved 6 decimal places by default ' 3.141593e+08 ' >>> format (314159267, ' 0.2e ') #科学计数法, specifies to retain 2 decimal places ' 3.14e+08 ' >>> format (314159267, ' 0.2E ') #科学计数法, specify to retain 2 decimal places, with uppercase E for ' 3.14E+08 ' >>> format (314159267, ' f ') #小数点计数法, default reserved 6 decimal places ' 314159267.000000 ' >> > Format (3.14159267000, ' f ') #小数点计数法, default reserved 6 decimal places ' 3.141593 ' >>> format (3.14159267000, ' 0.8f ') #小数点计数法, Specifies to retain 8 decimal places ' 3.14159267 ' >>> format (3.14159267000, ' 0.10f ') #小数点计数法, specifying 10 decimal places reserved' 3.1415926700 ' >>> format (3.14e+1000000, ' F ') #小数点计数法, Infinity is converted to the size of the letter ' INF ' #g的格式化比较特殊, assuming p is the reserved number of decimal digits specified in the format, First try to format the scientific notation, get the power exponent exp, if -4<=exp<p, then the fractional count method, and retain p-1-exp decimal place, otherwise counted by the fractional count method, and by p-1 reserved decimal places >>> format ( 0.00003141566, '. 1g ') #p =1,exp=-5 = = " -4<=exp<p not established, counted by scientific notation, reserved 0 decimal places ' 3e-05 ' >>> format (0.00003141566, ' .2g ') #p =1,exp=-5 = = " -4<=exp<p not established, counted by scientific notation, reserved 1 decimal places ' 3.1e-05 ' >>> format (0.00003141566, '. 3g ') #p =1,exp =-5 = = " -4<=exp<p not established, count by scientific counting method, keep 2 decimal point ' 3.14e-05 ' >>> format (0.00003141566, '. 3G ') #p =1,exp=-5 = =" -4< =exp<p not established, counted by scientific notation, reserved 0 decimal places, e use uppercase ' 3.14E-05 ' >>> format (3.1415926777, '. 1g ') #p =1,exp=0 = = " -4<=exp< P was established, counted by fractional notation, reserved 0 decimal places ' 3 ' >>> format (3.1415926777, '. 2g ') #p =1,exp=0 = = " -4<=exp<p established, counted by fractional count, reserved 1 decimal points ' 3 .1 ' >>> format (3.1415926777, '. 3g ') #p =1,exp=0 = = " -4<=exp<p set up, counted by fractional notation, reserved 2 decimal places ' 3.14 ' >>> Format (0.00003141566, '. 1n ') #和g相同 ' 3e-05 ' >>> format (0.00003141566, '. 3n ') #和g相同 ' 3.14e-05 ' >>&Gt Format (0.00003141566) #和g相同 ' 3.141566e-05 '