Python's format formatting function

Parameter passing method

The basic syntax is to use {} and: to replace the previous %.

>>> "output {:.2f}".format(3.1415926)
'output 3.14'
>>> "output %.2f" % 3.1415926
'output 3.14'

The format function can accept an unlimited number of parameters, and the positions can be out of order.

>>> "{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
'hello world'
>>> "{0} {1}".format("hello", "world")  # 设置指定位置
'hello world'
>>> "{1} {0} {1}".format("hello", "world")  # 设置指定位置
'world hello world'

You can also set parameters.

>>> "name: {name}, address: {address}".format(name="Looking", address="somewhere")
'name: Looking, address: somewhere'

>>> my_dict = {"name": "Looking", "address": "somewhere"}
>>> "name: {name}, address: {address}".format(**my_dict)
'name: Looking, address: somewhere'

>>> my_list = ["Looking", "somewhere"]
>>> "name: {0[0]}, address: {0[1]}".format(my_list)
'name: Looking, address: somewhere'

>>> my_list = ["Looking", "somewhere"]
>>> "name: {0}, address: {1}".format(*my_list)
'name: Looking, address: somewhere'

In fact, the new f-string after Python 3.6 is also very popular, and it is also very convenient to use. You can try it out.

>>> name = "Looking"
>>> address = "somewhere"
>>> "name: {name}, address: {address}"
'name: {name}, address: {address}'
>>> f"name: {name}, address: {address}"
'name: Looking, address: somewhere'

>>> my_dict = {"name": "Looking", "address": "somewhere"}
>>> f"name: {my_dict['name']}, address: {my_dict['address']}"
'name: Looking, address: somewhere'

>>> my_list = ["Looking", "somewhere"]
>>> f"name: {my_list[0]}, address: {my_list[1]}"
'name: Looking, address: somewhere'

Number formatting

^, <,> are centered, left-aligned, and right-aligned respectively, followed by a width, and the character with padding after the: sign can only be one character. If it is not specified, the default is to pad with spaces.

+ Means to display + before a positive number, and-before a negative number; (space) means to add a space before the positive number

b, d, o, and x are binary, decimal, octal, and hexadecimal, respectively.

Two decimal places

>>> "output {:.2f}".format(3.1415926)
'output 3.14'

Signed to keep two decimal places

>>> "output {:+.2f}".format(3.1415926)
'output +3.14'
>>> "output {:+.2f}".format(-3.1415926)
'output -3.14'

Without decimals (can be understood as rounding)

>>> "output {:.0f}".format(3.1415926)
'output 3'
>>> "output {:.0f}".format(-3.1415926)
'output -3'
>>> "output {:.0f}".format(2.71828)
'output 3'
>>> "output {:.0f}".format(-2.71828)
'output -3'

Number zero padding (right aligned, left padding, width 3)

>>> "output {:0>3d}".format(3)
'output 003'

Space padding (right-aligned, padding left, width is 3) 

>>> "output {: >3d}".format(3)
'output   3'

Number zero padding (left-aligned, padding to the right, width is 3)

>>> "output {:0<3d}".format(3)
'output 300'

Space padding (left alignment, padding on the right, width 3) 

>>> "output {: <3d}".format(3)
'output 3  '

Comma separated number format

In line with the style of western countries' thousandths

>>> "output {:,}".format(300000000000)
'output 300,000,000,000'

Percentage format

>>> "output {:.2%}".format(0.25)
'output 25.00%'
>>> "output {:.3%}".format(0.25)
'output 25.000%'

Index formatting

>>> "output {:.2e}".format(2500000)
'output 2.50e+06'

Alignment (left, center, right)

>>> "output {: <9d}".format(3)
'output 3        '
>>> "output {: ^9d}".format(3)
'output     3    '
>>> "output {: >9d}".format(3)
'output         3'

Base conversion

>>> "output {:b}".format(11)  # 二进制
'output 1011'
>>> "output {:o}".format(11)  # 八进制
'output 13'
>>> "output {:d}".format(11)  # 十进制
'output 11'
>>> "output {:x}".format(11)  # 十六进制(小写)
'output b'
>>> "output {:X}".format(11)  # 十六进制(大写)
'output B'

 

Guess you like

Origin blog.csdn.net/TomorrowAndTuture/article/details/114317286
Recommended