There are two ways to format strings in Python: percent sign method and format method
The percent sign method is relatively old, while the format method is a more advanced method and is intended to replace The ancient way, currently both coexist.
Percent mode
%[(name)][flags][width].[precision]typecode
(name) Optional, used to select the specified key
Flags optional, the value available for the choice is:
# Right; add the positive number before the positive number, and the negative number before the negative number; Add a negative sign;
space right-aligned; add a space before a positive number and a minus sign before a negative number;
0 Right-aligned; unsigned before a positive number and a minus sign before a negative number; use 0 Fill in the blank space
width Optional, occupy the width
.precision Optional, the number of digits left after the decimal point
typecode Required
s, get Pass in the return value of the __str__ method of the object and format it to the specified location
r, get the return value of the __repr__ method of the incoming object and format it to the specified location
c, integer: convert the number into its unicode corresponding value, the decimal range is 0 <= i <= 1114111 (py27 only supports 0-255); character: add characters to the specified Position
o, convert the integer to octal representation, and format it to the specified position
x, convert the integer to hexadecimal representation, and format it to Specify the position
d, convert the integer and floating point number into decimal representation, and format it to the specified position
e, convert the integer and floating point number into scientific notation, and format it Format to the specified position (lowercase e)
E, convert integers and floating point numbers into scientific notation, and format them to the specified position (uppercase E)
f, convert the integer , convert floating-point numbers into floating-point numbers, and format them to the specified position (default retains 6 digits after the decimal point)
F, same as above
g, automatically adjust to convert integers and floating-point numbers into floating point or scientific notation (use scientific notation for more than 6 digits), and format it to the specified position (e if it is scientific notation;)
G, automatically adjust the integer , convert the floating point number into floating point or scientific notation (more than 6 digits use scientific notation), and format it to the specified position (if it is scientific notation, it is E;)
%, When there is a formatting flag in the string, you need to use %% to represent a percent sign
Note: There is no way to automatically convert an integer into a binary representation in percent sign formatting in Python
tpl = "i am %s" % "alex" tpl = "i am %s age %d" % ("alex", 18) tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} tpl = "percent %.2f" % 99.97623 tpl = "i am %(pp).2f" % {"pp": 123.425556, } tpl = "i am %.2f %%" % {"pp": 123.425556, }
[[fill]align][sign][#][0][width][,][.precision][type]
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex') tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) tpl = "i am {0}, age {1}, really {0}".format("seven", 18) tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) 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}) tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) tpl = "i am {:s}, age {:d}".format(*["seven", 18]) tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
The above is the detailed content of String formatting % vs format which is better?. For more information, please follow other related articles on the PHP Chinese website!