There are two ways to format a Python string: a percent-semicolon and a format method.
Percent semicolons are relatively old, and format is more Advanced.
1, percent of the way
%[(name)][flags][width]. [precision]typecode
- (name) optional, used to select the specified key
- Flags are optional, and the values to choose from are:
- + right-aligned, positive plus, negative before plus minus;
- -left-justified; no sign before positive number, plus minus sign before negative number;
- Spaces are right-justified, plus a positive number before a negative number plus a minus sign;
- 0 Right alignment, no sign before positive number, minus sign before negative, and 0 padding in blank
- Width selectable, occupied widths
- . Precision optional, number of digits retained after the decimal point
- TypeCode must choose
- s, gets the return value of the __str__ method of the Passed-in object and formats it to the specified location
- r, gets the return value of the __repr__ method of the Passed-in object and formats it to the specified location
- c, Integer: converts a number to its Unicode corresponding value, 10 binary range is 0 <= i <= 1114111 (py27 only supports 0-255); character: add character to specified position
- o, converts an integer to an octal representation and formats it to a specified location
- x, converts an integer to a hexadecimal representation and formats it to a specified location
- d, converts an integer, floating-point number to a decimal representation, and formats it to a specified position
- e, converting integers, floating-point numbers to scientific notation, and formatting them to a specified location (lowercase e)
- e, converting integers, floating-point numbers into scientific notation, and formatting them to a specified position (uppercase E)
- 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)
- F, Ibid.
- 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 scientific counts are e;)
- 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 scientific counts are e;)
- %, when there is a format flag in the string, a percent sign is required
Note: There is no way to automatically convert an integer to a binary representation in Python with a Percent-semicolon format
Common Formatting:
1TPL ="I am%s"%"Alex"2 3 4 5TPL ="I am%s age%d"% ("Alex", 18)6 7 8 9TPL ="I am% (name) s age percent (age) D"% {"name":"Alex"," age": 18}Ten one a -TPL ="percent%.2f"% 99.97623 - the - -TPL ="I am% (pp). 2f"% {"pp": 123.425556, } - + - +TPL ="I am%.2f percent"% {"pp": 123.425556,}
2. Format mode
[[fill]align] [sign] [#] [0] [width] [,] [. precision] [type]
- Fill "optional" white space filled with characters
- Align "optional" alignment (required with Width)
- <, content left Justified
- Content right-aligned (default)
- =, The content is right-aligned, the symbol is placed to the left of the padding character, and only valid for numeric types. Even: symbol + filler + number
- ^, content Centered
- Sign "optional" with unsigned numbers
- +, positive home plus, minus plus negative;
- -the plus sign does not change, minus is negative;
- spaces, plus spaces, minus signs and negative;
- # "optional" for binary, octal, hex, If added #, 0b/0o/0x is displayed, otherwise not displayed
- , "optional" adds a delimiter to the number, such as: 1,000,000
- Width "optional" Format the size of the placeholder
- . precision "optional" decimal digits reserved precision
- Type "optional" formatting types
- Parameters passed into the string type
- s, format string Type data
- blank, No type specified, default is none, same as S
- passed in the parameter of "integer type"
- b, Automatically converts a 10-based integer to a 2-binary representation and then formats the
- c, Automatic conversion of 10 binary integers to their corresponding Unicode characters
- d, decimal integer
- o, The 10-binary integer is automatically converted to 8-binary representation and then formatted;
- x, automatic conversion of 10 binary integers to 16 binary representation and then formatting (lowercase x)
- x, Automatically converts a 10 binary integer to a 16 binary representation and then formats (uppercase x)
- Parameters passed in floating-point or decimal type
- e, converted to scientific notation (lowercase e), and then formatted;
- e, converted to scientific notation (capital e), and then formatted;
- f, converted to floating-point type (6 digits after the default decimal point), and then formatted;
- F, converted to floating-point type (6 digits after the default decimal point), and then formatted;
- g, automatically switch between E and F
- G, automatically switch between E and F
- %, Display percent (default 6 digits after Decimal)
Common Formatting:
1TPL ="I am {}, age {}, {}". Format ("Seven", 18,'Alex')2 3 4 5TPL ="I am {}, age {}, {}". Format (*["Seven", 18,'Alex'])6 7 8 9TPL ="I am {0}, age {1}, really {0}". Format ("Seven", 18)Ten one a -TPL ="I am {0}, age {1}, really {0}". Format (*["Seven", 18]) - the - -TPL ="I am {name}, age {age}, really {name}". Format (name="Seven", age=18) - + - +TPL ="I am {name}, age {age}, really {name}". Format (**{"name":"Seven"," age": 18}) a at - -TPL ="I am {0[0]}, age {0[1]}, really {0[2]}". Format ([1, 2, 3], [11, 22, 33]) - - - inTPL ="I am {: s}, age {:d}, Money {: f}". Format ("Seven", 18, 88888.1) - to + -TPL ="I am {: s}, age {:d}". Format (*["Seven", 18]) the * $ Panax NotoginsengTPL ="I am {name:s}, age {age:d}". Format (name="Seven", age=18) - the + aTPL ="I am {name:s}, age {age:d}". Format (**{"name":"Seven"," age": 18}) the + - $TPL ="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) $ - - theTPL ="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) - Wuyi the -TPL ="numbers: {0:b},{0:o},{0:d},{0:x},{0:x}, {0:%}". Format (15) wu - about $TPL ="numbers: {num:b},{num:o},{num:d},{num:x},{num:x}, {num:%}". Format (num=15)
More Formatting operations: https://docs.python.org/3/library/string.html
Python string formatting