Python built-in functions (23) -- format, pythonformat

Source: Internet
Author: User

Python built-in functions (23) -- format, pythonformat

English document:

format(Value[,Format_spec])

ConvertValueTo a "formatted" representation, as controlledFormat_spec. The interpretationFormat_specWill depend on the type ofValueArgument, however there is a standard formatting syntax that is used by most built-in types: Format Specification Mini-Language.

The defaultFormat_specIs an empty string which usually gives the same effect as callingstr(value).

A callformat(value, format_spec)Is translatedtype(value).__format__(value, format_spec)Which bypasses the instance dictionary when searching for the value's__format__()Method.TypeErrorException is raised if the method search reachesobjectAndFormat_specIs non-empty, or if eitherFormat_specOr the return value are not strings.

 

Note:

 

1. The function Format a value.

2. If the format_spec parameter is not provided, it will have the same effect as calling str (value) and convert it to string format.

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

3. for different types, the values provided by the format_spec parameter are different.

# The parameter 's' None> format ('some string', 's') 'some string'> format ('some string ') 'Some string' # The following parameters can be provided for integer values: 'B' C' d ''o' x' 'n' None >>> format (3, 'B') # convert to binary '11'> format (97, 'C') # convert unicode to character 'a'> format (11, 'D') # convert to a 10-digit '11'> format (11, 'O') # convert to an 8-digit '13'> format (11, 'X') # convert to a hexadecimal lowercase letter to 'B'> format (11, 'x ') # convert to hexadecimal uppercase letters to 'B' >>> format (11, 'n') # Same as d '11' >>> format (11) # The default value is the same as that of d. '11' # Floating Point Numbers can provide the following parameters: 'E' E' F' ''G'' 'n' '%' None. >>> format (314159267, 'E') # scientific notation. The default value is 6 decimal places. 141593e + 08 '> format (314159267, '0. 2e') # scientific notation, specifying to retain 2 decimal places '3. 14e + 08 '> format (314159267, '0. 2e') # scientific notation, specifying to retain 2 decimal places, using uppercase E to represent '3. 14E + 08 '> format (314159267, 'F') # decimal point counting method. The default value is 6 decimal places '123. 000000 '>>> format (3.14159267000, 'F') # number of decimal points. The default value is 6 decimal places. 141593 '> format (3.14159267000, '0. 8f') # decimal point counting method, which specifies to keep 8 decimal places '3. 14159267 '> format (3.14159267000, '0. 10f') # decimal point notation, which specifies to retain 10 decimal places. 1415926700 '>>> format (3.14e + 1000000, 'F') # indicates the decimal point count. The infinite number is converted to the 'inf' # g format, assume that p is the number of reserved decimal places specified in the format. First, use the scientific notation to format it to obtain the power index exp. If-4 <= exp <p, use the decimal notation, and keep the p-1-exp decimal places, otherwise count by decimal notation, and retain the decimal places by p->> format (0.00003141566 ,'. 1G ') # p = 1, exp =-5 = "-4 <= exp <p is not true, counting by scientific notation, retain the 0 decimal point '3e-05 '> format (0.00003141566 ,'. 2G ') # p = 1, exp =-5 = "-4 <= exp <p is not true, count by scientific notation, retain 1 decimal point '3. 1e-05 '>>> format (0.00003141566 ,'. 3G ') # p = 1, exp =-5 = "-4 <= exp <p is not true, count by scientific notation, retain 2 decimal places '3. 14e-05 '>>> format (0.00003141566 ,'. 3G ') # p = 1, exp =-5 = "-4 <= exp <p is not true, count by scientific notation, retain 0 decimal places, e. Use uppercase letters. 14E-05 '>>> format (3.1415926777 ,'. 1G ') # p = 1, exp = 0 = "-4 <= exp <p is true, counting by decimal notation, keep the 0 decimal point '3'> format (3.1415926777 ,'. 2G ') # p = 1, exp = 0 = "-4 <= exp <p is true, count by decimal notation, retain 1 decimal point '3. 1 '> format (3.1415926777 ,'. 3G ') # p = 1, exp = 0 = "-4 <= exp <p is true, count by decimal notation, retain 2 decimal places '3. 14'> format (0.00003141566 ,'. 1n') # Same as g '3e-05 '> format (0.00003141566 ,'. 3n') # The same as g '3. 14e-05 '>>> format (0.00003141566) # Same as g' 3. 141566e-05'

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.