String formatting
There are two ways to format a Python string: a percent-semicolon, format-mode
In the two string formats,% is the previous string format, and Python has introduced the format string formatting in order to have more operations on string formatting. Now there are two ways to format the string, the format string is currently formatted in many ways, it is possible that format will replace%.
Percent Semicolon mode
Grammar:
%[(name)][flags][width]. [Precision]typecode
- u [(name)] optional, can be used to select the specified key
- u [flags] are optional and the values available for selection are:
- L + Right alignment, positive plus, negative before plus minus;
- L-left-justified, positive sign unsigned, negative before minus
- L Space Right-justified, positive number with a space before negative plus minus
- L 0 Right alignment; no sign before positive number, minus sign before negative; Fill in blank with 0
- u [width] optional, occupied width
- U. Precision optional, number of digits retained after the decimal point
- U typecode must be selected
- L S. Gets the return value of the _str_ method of the passed-in object and formats it to the specified location
- L R. Gets the return value of the _repr_ method of the passed-in object and formats it to the specified location
- L c. Integers: Values corresponding to the number Converter's Unicode, 10 in 0<=i<=1114111 (Py27 0-255); character: adding characters to the specified position
- L O, converts an integer to an octal representation and formats it to a specified location
- L x, converts an integer to a hexadecimal representation and formats it to a specified location
- L D, converts an integer, floating-point number to a decimal representation, and formats it to a specified position
- l e, convert integer, floating-point number to scientific notation and format it to the specified position (lowercase e)
- l e, convert integer, floating-point number to scientific notation and format it to the specified position (capital E)
- L F, converts an integer, floating-point number to a floating-point number representation, and formats it to a specified position (the default is 6 digits after the decimal point)
- L F, ibid.
- L g, auto-adjust convert integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and format them to a specified location (if the scientific count is E;)
- L G, Auto-adjust convert integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and format them to a specified location (if the scientific count is E;)
- L%, when there is a format flag in the string, you need to represent a percent sign with
Examples of commonly used string formatting:
Demo = "I am%s"% "Jack" demo = "I am%s age%d"% ("Jack", +) demo = "I am% (name) s age% (age) d"% {"name": "Jack", "age ":" demo = "percent%.2f"% 99.97623demo = "I am% (PP)." 2f "% {" pp ": 123.425556,} demo =" I am%.2f percent "% {" pp ": 123.42 5556,}
Python Study day5 Basic article