See Official document Https://docs.python.org/3/library/string.html
There are two ways to format a Python string: a percent-semicolon, format-mode
1, Percent semicolon mode%[(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
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
Parameters passed into the integer type
-
-
- B, automatic conversion of 10 binary integers to 2 binary representation and then formatting
- C, automatic conversion of 10 binary integers to their corresponding Unicode characters
- d, decimal integer
- O, the 10 binary integers are automatically converted to 8 binary representation and then formatted;
- X, automatically converts 10 binary integers to 16 binary representation and then formats (lowercase x)
- X, automatically converts 10 binary integers to 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)
Python string formatting