Home >Backend Development >Python Tutorial >String formatting % vs format which is better?
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 e09c94038672537073afeb8b5f1bbdb7, content is aligned to the right (default)
=, content is aligned to the right, the symbol is placed to the left of the padding character, and only numbers are The type is valid. Even if: symbol filler number ^, the content is centered sign [Optional] There are unsigned numbers , plus the positive sign plus the negative sign, and the negative sign plus the negative; -, the positive sign remains unchanged, the negative sign is added to the negative; Space, the positive sign is a space, the negative sign is added to the negative; # Hexadecimal, if # is added, 0b/0o/0x will be displayed, otherwise will not be displayed, [Optional] Add a separator for the number, such as: 1,000,000width [Optional] 】Width occupied by formatting bits.precision [Optional] Decimal digit retention precisiontype [Optional] Formatting typeInput "string type" Parameterss, format string type dataBlank, if the type is not specified, the default is None, the same as sPass in the "integer type" parameterb, automatically convert the decimal integer into binary representation and then format it c, automatically convert the decimal integer into its corresponding unicode character d , decimal integer o, automatically convert the decimal integer into octal representation and then format it; x, automatically convert the decimal integer into hexadecimal representation and then format it (lowercase x) X, automatically convert the decimal integer into hexadecimal representation and then format it (uppercase X) Pass in the parameter of "floating point or decimal type"e, Convert to scientific notation (lowercase e), and then format; E, Convert to scientific notation (uppercase E), and then format;f, converted to floating point type (default 6 digits after the decimal point), then formatted; F, converted to floating point type (default 6 digits after the decimal point), then formatted; g, automatically switch between e and fG, automatically switch between E and F%, display percentage (default display is 6 digits after the decimal point)
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!