Python second-level compulsory function. format() function

Table of contents

1. Introduction to the format() function

2. Function application

① Position filling

②Fill, align, width

③The sign parameter retains the sign

 ④ Specify the precision (.nf), separator (,), and base (o, b, d, x)


1. Introduction to the format() function

The format() function is used to collect subsequent positional parameters and key field parameters, and fill the placeholders in the string with their values. Usually the format() function cooperates with the print() function to achieve strong formatted output capabilities. The format() function is a compulsory subject in the Python Level 2 exam. It is the first question in the Python Level 2 answer and it is also a difficult one simple subject.

The fixed output format of the format() function is as follows:

'{key : fill, align, sign,0,width, precision, type}'.format(para1...)

General introduction:

The simple format form of the format() function is '{}'.format().

It uses the form of curly braces ('{}') in the middle of quotation marks to make a function call through the form of dot (.) format().

Parameter introduction:

1.fill: used to specify the fill character, the default is a space

2.align: Specifies the alignment: > is right alignment, < is left alignment, ^ is center alignment

3. sign: Specify whether to keep the sign: + to keep the sign, - to keep only the minus sign

4.0: If you add 0 in front of the width, it means fill with 0

5.width: specify the width

6.precision: specify the precision

7.type: specify the type, such as b for binary, x for hexadecimal

2. Function application

① Position filling

The syntax format is as follows:

'{0}{1}'.format(part1,part2)

When the format() function is filled, it can be filled according to the position parameters. When 0 and 1 are exchanged to {1}{0}, the front position is filled with the parameter content of part2, and the latter position is filled with the content of part1. Example:

position 0,1

'Hello,{0}. My name is {1}. How\'s it going?'.format('Hialry','Vergil')

position 1,0

'Hello,{1}. My name is {0}. How\'s it going?'.format('Hialry','Vergil')

Note: If the position 0, 1 is not specified, that is, when the previous parameters are omitted, the filling of the function will be filled according to the order. That is, the part1 parameter will fill the first curly brace and so on

In addition to fixed position filling, you can also use keyword filling

'I\'m {name1}, and I miss u so much, {name2}.'.format(name1='vergil',name2='hilary')

②Fill, align, width

Fill (fill): Filled items, here are the characters, numbers, etc. used for filling. It is worth noting that filling in quotation marks, brackets, etc. needs to be escaped with \ escape character.

Alignment (align): There are three alignment methods: Center (^), > for right alignment, < for left alignment

Width (width): Fill in the parameter as the data, that is, the parameter that occupies the position

'{:=^20}'.format('bb')

③The sign parameter retains the sign

The quotes used for this padding therefore need to be escaped with the escape character \, which is placed after the \.

n=100
'{:\'^+30}'.format(n)

#不保留正号,仅保留负号
'{:\'^30}'.format(n)
'{:\'^-30}'.format(n)

 

 ④ Specify the precision (.nf), separator (,), and base (o, b, d, x)

o,b,d,x: binary, octal, decimal, hexadecimal

'{:\'^+20b}'.format(n)

The parameters of the precision control are set to .nf

The value of n is related to the reserved digits after the decimal point. This reserved method is a rounding method. At the same time, when the reserved digits are insufficient, 0 is used to fill the subsequent positions.

Thousands separator: , used for decimal thousands separator

'{:\'^+20,}'.format(n)

 

 Note: Thousands and decimal values ​​cannot be used at the same time, and ValueError: Cannot specify ',' with 'b' will appear if used at the same time. Error, the thousand separator is a number used in decimal

Guess you like

Origin blog.csdn.net/Sheenky/article/details/125036176