String formatting
There are two ways to format a Python string: a percent-semicolon, format-mode
The percent of the semicolon is relatively old, and format is a more advanced way to replace the old way, the two coexist.
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
Practice:
" I am%s " " Tiantian " Print (TP1)
Output Result:
I am Tiantian
TP2 ="I am%s age%d"%("Tiantian", 19)Print(TP2) TP3="I am% (name) s age percent (age) D"% {'name':'Tiantian',' Age': 18}Print(TP3)
Output Result:
I am Tiantian age 19
I am Tiantian age 18
Right alignment:
TP4 = % (name) +30s age% (age) d "% {" name ' : ' tiantian : 18 ' print (TP4)
Output:
Tiantian Age
# Align Left " % (name) -30s Age percent (age) D" % {'name':'tiantian' ,'age': '+ 'print(TP5)
Output Result:
Tiantian age 18
# keep two decimal places, rounding " % (a). 2f" %{'a': 1.37834}print(TP6)
Results:
1.38
Common formatting:
TPL ="I am%s"%"Alex"TPL="I am%s age%d"% ("Alex", 18) TPL="I am% (name) s age percent (age) D"% {"name":"Alex"," Age": 18}TPL="percent%.2f"% 99.97623#TPL ="I am% (PP). 2f"% {"pp": 123.425556}Print(TPL) Output:
I am 123.43TPL="I am% (PP). 2f"% {"pp": 123.425556,}Print(TPL)
Output:
I am 123.43%
python-string Formatting