Home > Backend Development > Python Tutorial > String formatting % vs format which is better?

String formatting % vs format which is better?

anonymity
Release: 2019-05-25 11:28:36
Original
2426 people have browsed it

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.

String formatting % vs format which is better?

Percent mode

%[(name)][flags][width].[precision]typecode
Copy after login

(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, }
Copy after login

Format method

[[fill]align][sign][#][0][width][,][.precision][type]
Copy after login
fill                                                                                                                                                                    

<, content is aligned to the left

>, 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,000

width [Optional] 】Width occupied by formatting bits

.precision [Optional] Decimal digit retention precision

type [Optional] Formatting type

Input "string type" Parameters

s, format string type data

Blank, if the type is not specified, the default is None, the same as s

Pass in the "integer type" parameter

b, 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 f

G, automatically switch between E and F

%, display percentage (default display is 6 digits after the decimal point)

tpl = "i am {}, age {}, {}".format("seven", 18, &#39;alex&#39;)
tpl = "i am {}, age {}, {}".format(*["seven", 18, &#39;alex&#39;])
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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template