Are you sure you don't need to read this Python 3 format?

Posted by matvespa on Tue, 28 Dec 2021 08:32:16 +0100

Column address: The strongest Python 3 Foundation

Official account: Python productivity

preface

The format function is actually an advanced version of the modulo operator (%), which is similar to the modulo operator (%). The difference is that you don't need to be like the modulo operator many times (%) to memorize the meaning represented by symbols of various output types. It can be formatted only by giving specific placeholders and corresponding parameters. It is a very convenient formatting function. Of course, it can be done if you want to specify the precision like the modulus operator (%).

1, Subscript index value

Compared to modulo operators (%) the format function has few restrictions on the various assignments of objects, that is, any data type can be placed in any position, whether it is an ordinary number or string type, or various iteratable collection types, such as lists, tuples, etc. you can see the following example. The format function is like storing a tuple and a pointer in the specified position Fixed data, each data has a specified subscript.

print('Integer:{0},Floating point number:{1},character string:{2}'.format(123, 52.36, 'Python'))
print('List:{0},Tuple:{1},Dictionaries:{2},Set:{3}'.format([1, 2, 3], (4, 5, 6), {'name': 'Python', 'price': 123.36}, {7, 8, 9}))

# Output > > >
Integer: 123, floating point number: 52.36,character string: Python
 List:[1, 2, 3],Tuple:(4, 5, 6),Dictionaries:{'name': 'Python', 'price': 123.36},Set:{8, 9, 7}

For the parameters in the parentheses of the format function, each filled element has specified a specific subscript. In the string, it is equivalent to using these subscripts to obtain the data at the specified position. You can not only obtain it once, but also continuously use the same subscript to obtain the same element.

# Take down the value marked 0 multiple times
print('Integer:{0},Number:{0},Positive integer:{0},Floating point number:{1},character string:{2}'.format(123, 52.36, 'Python'))

# Output > > >
Integer: 123, number: 123, positive integer: 123, floating point number: 52.36,character string: Python

# ==============================================================================================

# String subscripts can be placed anywhere
print('Floating point number:{1},character string:{2},Integer:{0},Number:{0},Positive integer:{0}'.format(123, 52.36, 'Python'))

# Output > > >
Floating point number: 52.36,character string: Python,Integer: 123, number: 123, positive integer: 123

# ==============================================================================================

# Take only some subscript content
print('character string:{2}'.format(123, 52.36, 'Python'))

# Output > > >
character string: Python

At the same time, the value beyond the subscript range will directly report an error.

print('character string:{3}'.format(123, 52.36, 'Python'))

# Output > > >
IndexError: Replacement index 3 out of range for positional args tuple

If a list is added to the parameter, the use method is similar to that of taking the list in the tuple. They all take the list at the corresponding position in the tuple first, and then take the elements in the list. Of course, you can also add multiple lists.

# Add a single list
print('{0[1]}, {0[2]}'.format([123, 52.36, 'Python']))

# Output > > >
52.36, Python

# =====================================================================

# Add multiple lists
print('{0[1]}, {0[2]}, {1[1]}'.format([123, 52.36, 'Python'], [456, 789]))

# Output > > >
52.36, Python

2, Empty braces direct value


In Python version 3.1, you can even use empty braces instead of writing numeric subscripts. In this case, python can automatically help us replace the value into the corresponding parentheses. However, it should be noted that since there is no specific subscript, python can only do one-to-one correspondence, that is, there can be no more brackets than parameters, and the location can only be determined according to the location of the parameters provided.

# Formatting that does not exceed the range of parameters
print('Integer:{},Floating point number:{},character string:{}'.format(123, 52.36, 'Python'))

# Output > > >
Integer: 123, floating point number: 52.36,character string: Python

# ==============================================================================

# Formatting that exceeds the range of parameters will directly report an error
print('Integer:{},Floating point number:{},character string:{},Number:{}'.format(123, 52.36, 'Python'))

# Output > > >
IndexError: Replacement index 3 out of range for positional args tuple

# ==============================================================================

# For formatting with fewer parameters than the number of parameters, redundant parameters will be ignored
print('Integer:{},Floating point number:{}'.format(123, 52.36, 'Python'))

# Output > > >
Integer: 123, floating point number: 52.36

You should be reminded that empty braces cannot be used with subscript braces. Python will directly report an error, because it may cause ambiguity for the program, so you must choose one of them.

print('Integer:{0},Floating point number:{1},character string:{}'.format(123, 52.36, 'Python'))

# Output > > >
ValueError: cannot switch from manual field specification to automatic field numbering

3, Keyword value

The format function can not only take values according to subscripts, but also store them in the form of key value pairs like a dictionary, and obtain the corresponding values according to keywords.

print('Integer:{intNum},Floating point number:{floatNum},character string:{str}'.format(intNum=123, floatNum=52.36, str='Python'))

# Output > > >
Integer: 123, floating point number: 52.36,character string: Python

If you get a nonexistent keyword (Key), an error will be reported directly.

print('Integer:{num}'.format(intNum=123, floatNum=52.36, str='Python'))

# Output > > >
KeyError: 'num'

If two identical keywords (keys) are defined in the parameter, an error will also be reported.

print('Integer:{intNum},Floating point number:{floatNum},character string:{str}'.format(intNum=123, floatNum=52.36, floatNum='Python'))

# Output > > >
SyntaxError: keyword argument repeated: floatNum

As mentioned above, empty braces and subscript braces cannot be used together. In fact, Key braces cannot be mixed with empty braces.

print('Integer:{intNum},Floating point number:{floatNum},character string:{}'.format(intNum=123, floatNum=52.36, str='Python'))

# Output > > >
IndexError: Replacement index 0 out of range for positional args tuple

In the subscript value above, it is mentioned that the list can be passed in the parameter, and we can also pass in the corresponding list or tuple when taking the keyword value.

# Insert list
print('{keyList[0]}'.format(keyList=[123, 456]))

# Output > > >
123

# ===============================================================================

# Insert tuple
print('{keyList[1]}'.format(keyList=(123, 456)))

# Output > > >
456

4, Mixed use

Since we mentioned the above three methods, one is to use subscript to value, The other is to use the Key to get the value, and the other is the empty braces. We said that empty braces and subscript braces cannot be mixed, and empty braces and braces with Key cannot be mixed. There are keys What's the problem with (Key) brackets and brackets with subscripts? In fact, the two can be mixed, but the identification cost and specification of mixed use are higher than those of the two used alone, so mixed use is generally not recommended.

# Use when the two positions are completely aligned
print('{0}, {1}, {a}'.format(1, 2, a=3))

# Output > > >
1, 2, 3

# ==============================================================================

# When the two positions are used with errors, an error will be reported directly, because the parameters of positions a and 1 actually coincide
print('{0}, {a}, {1}'.format(1, a=3, 2))

# Output > > >
SyntaxError: positional argument follows keyword argument

5, Format of the format function

Through the above examples, you can have a general understanding of the format of the format function. Because the format function is more like a supplement to the format of the modular operator, the format function also has some other functions that the format of the modular operator does not have. First, look at the format of the format function below.

{[<name>][!<conversion>][:[[<fill>]<align>][<sign>][#][0][<width>][<group>][.<prec>][<type>]]}

You can see the format of the format function shown above. In fact, many things can be specified, such as the alignment specified by < align > or the grouping specified by < group >. Compared with the modulus operator formatting method, it has more perfect specified content.

Although the content specified by the format function seems a little more, in fact, the content of this format can be divided into two parts. One part is in front of the colon, which will be used less frequently, and the specified content behind the colon in the second part will be more common in normal use. Let's divide this format into two parts.

General format:{[<name>][!<conversion>][:<format_spec>]}

<format_spec>part:[[<fill>]<align>][<sign>][#][0][<width>][<group>][.<prec>][<type>]

As you can see, the format is divided into two parts. The most important part is < format_ Spec >, which will be explained in detail below. Let's take a look at the < Format > in the table_ Detailed explanation of each component in spec > section.

Component nameinterpretation
<fill>When specifying the width, if there is more space than the string, specify the filling content
<align>When specifying the width, if there is more space than the string, specify the alignment of the remaining strings
<sign>Specify prefix symbol
#When the specified type is binary, octal, hexadecimal, the basic indicator is displayed on the left
0When specifying the width, if there is more space than the string, fill 0 in the left space instead of ASCII space characters
<width>Specifies the minimum width of the output content
<group>If the content is a number, specify the character of the number group
.<prec>Specify the number of decimal places to output after the floating point number (be sure to add a decimal point)
<type>Specify the type of output content, that is, convert the passed in parameters to a specific type

6, Usage of each component

1. type component: Specifies the type

For the last < type > component, you can refer to Python 3 string formatting - modulus operator (%) The specified type is as like as two peas formatting methods. But it should be noted that there is a slight difference in some specified contents. Let's look at the difference between the two methods of format function and the formatting method of the module operators for the <type> component specified method.

print('%f' % 2.123)
print('{:f}'.format(2.123))

# Output > > >
2.123000
2.123000

# =============================================================================

print('%d' % 5)
print('{:d}'.format(5))

# Output > > >
5
5

# =============================================================================

print('%s' % 'Python')
print('{:s}'.format('Python'))

# Output > > >
Python
Python

# =============================================================================

print('%f%%' % 2.123)
print('{:%}'.format(0.02123))

# Output > > >
2.123000%
2.123000%

As shown in the above example, except that the method formats specified by the two methods are different, the specified symbols are common. For example, if you specify a number, just fill in d. Next, let's take a look at the difference between the two methods mentioned above. In particular, the last output percentage sign above requires a specific conversion in the modulus operator formatting method to output correctly, but not in the format function.

Specify typeformat functionModular operator
i,uI won't support itDecimal integer conversion
cSingle character conversion, can only be an integerSingle character conversion, which can be an integer or a single character value
rNot supported, in< This function is provided in the conversion > componentString (display with repr())

Here are some examples of these differences.

(1) i, u type

print('%i' % 20)

# Output > > >
20

# =============================================================================

print('{:i}'.format(20))

# Output > > >
ValueError: Unknown format code 'i' for object of type 'int'

(2) c type

print('%c' % 38)
print('%c' % 'i')

# Output > > >
&
i

# =============================================================================

print('{:c}'.format(38))
print('{:c}'.format('i'))

# Output > > >
&
ValueError: Unknown format code 'c' for object of type 'str'

(3) r type

The specified type r appears separately in the < conversion > position in the format function. It is not a type selection like the format of the modulus operator. It is equivalent to the repr() function.

print('{!r}'.format('123'))
print('%r' % '123')

# Output > > >
'123'
'123'

2. fill and align components: specify the filling content or alignment in the spare part

The < fill > and < align > components only take effect when the specified output character width exceeds the existing character. If the output character is as wide as the original character, it is useless to specify the character for filling. Moreover, if no output string has no specified width, these two components will be directly ignored.

Here, let's take a look at the values that can be taken by the < align > component. In general, there are only a few alignment methods, including left alignment, right alignment and center alignment. These are also used in the < align > component. Of course, an alignment at both ends is added.

Symbolinterpretation
>Right align
<Align left
^Center alignment
=Align both ends

Left alignment, right alignment and center alignment are easy to understand. Here are some examples of these alignment methods. Note that the length of the output string must exceed the length of the original string before it takes effect.

print('{:>10s}'.format('Python'))
print('{:<10s}'.format('Python'))
print('{:^10s}'.format('Python'))

# Output > > >
    Python
Python
  Python

As can be seen from the above examples, the output contents are different with different alignment methods. Of course, this output method is not easy to distinguish whether it has been aligned according to the requirements. We can try to add filling content in front of the specified alignment method and fill the spare part with the specified characters. This part is the content of the < fill > component, Here, for the convenience of observation, let's give an example.

print('{:*>10s}'.format('Python'))
print('{:*<10s}'.format('Python'))
print('{:*^10s}'.format('Python'))

# Output > > >
****Python
Python****
**Python**

In the above example, fill the empty parts with asterisks so that it is easy to tell whether they have been aligned as required. In addition, there is a method of aligning both ends. This method can only be used on numbers, and the numbers can only take effect when symbols are added. Otherwise, an error will be reported. The specified content of the symbols is specified by the < sign > component. Here is an example. As you can see, in the following example, if the two ends alignment is specified, the rest of the middle will be filled in with the default space.

print('{:=+10d}'.format(123))
print('{:=10d}'.format(-123))

# Output > > >
+      123
-      123

The content of the < fill > component has been exemplified in the above example, that is, fill the spare part with the specified content. Of course, these will take effect only when the length of the output string is longer than the length of the original string. Let's take a look at whether the specified width will take effect when it is smaller or the same width as the original character. Note that the < fill > component cannot be used alone without the < align > component. If it is used alone, an error will be reported.

# Error reporting when used alone
print('{:*10s}'.format('Python'))

# Output > > >
ValueError: Sign not allowed in string format specifier

# =============================================================================

# Correct use method
print('{:*>10s}'.format('Python'))
print('{:-^10s}'.format('Python'))

# Output > > >
****Python
--Python--

3. sign component: Specifies the number prefix symbol

The < sign > component can only be used in the case of digital output. In our general impression, the prefix symbol of numbers only has a positive sign (+) or a negative sign (-). Spaces can also be specified here. The specified spaces are generally indistinguishable and can be found only when used together with the < fill > component.

print('{:+d}'.format(456))
print('{:-d}'.format(456))
print('{:-d}'.format(-456))
print('{:d}'.format(-456))

# Output > > >
+456
456
-456
-456

In the above example, when a positive sign is added, a + sign will be added in front of the number, while a negative sign will not. The negative sign will only have an effect when a negative number is specified. In this case, it is no different from whether to add or not, so the negative sign can generally not be added.

In addition, you can take a look at the previous example of adding spaces, which will be used together with the < fill > component for easy observation. As can be seen from the example, in the output result, in addition to the parts filled with asterisks, there is a blank space, which is the space specified by the < sign > component.

print('{:*> 8d}'.format(456))
print('{:*^ 8d}'.format(456))

# Output > > >
**** 456
** 456**

4. # component: display binary, octal and hexadecimal indicators

In the format function, the < type > types of binary, octal and hexadecimal are specified as b, o and x respectively. The three specified types will respectively convert the incoming data into the specified type of hexadecimal data. Because we usually use decimal data, here are examples of converting decimal data into other three types of data.

print('{:b}'.format(50))
print('{:o}'.format(50))
print('{:x}'.format(50))

# Output > > >
110010
62
32

You can see that in the example, the incoming decimal system is converted into corresponding numbers, but sometimes the results alone can not tell what decimal system these numbers represent. Here, you can add a #, before the type specification, to force the output of decimal symbols.

print('{:#b}'.format(50))
print('{:#o}'.format(50))
print('{:#x}'.format(50))

# Output > > >
0b110010
0o62
0x32

5. 0 components: fill the spare parts

The 0 component has the same effect as the < fill > component, except that the < fill > component can specify the filled symbol arbitrarily, while the 0 component can only fill in the number 0.

print('{:*>10s}'.format('Python'))
print('{:>010s}'.format('Python'))

# Output > > >
****Python
0000Python

6. width component: specify the width

In the above examples, we have seen how to use the specified width component. By limiting the width of the string, the redundant part of the output will be filled with spaces. Moreover, there is a difference between the% formatting method and the format function, which limit the width of the string.

print('%8s' % 'Python')
print('{:8s}'.format('Python'))

# Output > > >
  Python
Python

7. group component: group numbers

When outputting data of this type like numbers as amounts, we may ask to add commas in the thousandth percentile for easy viewing. In the format function, you can directly use commas or underscores to separate numbers.

print('{:,d}'.format(123456789))
print('{:_d}'.format(123456789))

# Output > > >
123,456,789
123_456_789

Note that only commas and underscores can be used to separate strings with symbols. If other symbols are used to separate strings, an error will be reported.

print('{:*d}'.format(123456789))

# Output > > >
ValueError: Invalid format specifier

8. .prec component: specifying precision

Specifying precision is often used to deal with numeric data. For example, when it comes to money calculation and output, several decimal places will be specified.

Moreover, as mentioned above, although the string can be formatted by specifying the output width, it will not be intercepted when the specified width is less than the width of the original string. Specifying the precision of the string can specify the length of the output string as the effect of string interception.

print('%.2f' % 2.123)
print('{:.2f}'.format(2.123))

# Output > > >
2.12
2.12

# ============================================================================

print('%.3s' % 'Python')
print('{:.3s}'.format('Python'))

# Output > > >
Pyt
Pyt

7, Use of name and conversion

After introducing the second half, let's take a look at the first half of the format. The format of the first half of the format is: [< name >] [! < conversion >], which is divided into two optional component parts, < name > and < conversion >.

1. name component: get the corresponding parameter value

In fact, we talked about the < name > component at the beginning. In fact, values are obtained through subscripts and keywords. What is filled in the < name > component is the subscript value or keyword.

print('{0:*>10s}, {1:.2}'.format('Python', 123.456))
print('{name:*>10s}, {price:.2}'.format(name='Python', price=123.456))

# Output > > >
****Python, 1.2e+02
****Python, 1.2e+02

2. conversion component: character type conversion

Conversion, as the name suggests, means conversion. In this component, there are three optional types for string conversion, which are! s,! r,! a. The three types correspond to str(), repr(), ascii(). Because it's basically not used very much, you just need to know about it here.

print('{!r}'.format(123))
print('{!a}'.format(123))
print('{!s}'.format(123))

# Output > > >
123
123
123

summary

The format function can be said to be an advanced version of the% operator format. The latter can do what the former can do, and the format function has a more intuitive feeling than the% operator. Moreover, the format function has three different value taking methods, and keyword value taking and subscript value taking can be mixed, but we must pay attention to the order of value taking. If the order is wrong, an error will be reported.

At the same time, in the format of the format function, there are various combination operation modes, which can not only specify the precision and width, but also specify the grouping mode. Compared with the modulus operator, the format function can meet higher formatting requirements.

Topics: Python Programming