In the process of learning strings, we have talked about a lot of usage methods before. In actual use, we often need to format strings and output the results we need through formatting.
Formatting a string is to create a space first, then leave a few positions in this space, and then fill in the corresponding content according to the needs. The positions left here are equivalent to placeholders. There are two ways to format a string. One is to use the % operator, and the other is to use the format() method.
The % operator is very convenient when we format strings. Its syntax structure is as follows:
'%[+][-][0][.m] format character'%iteration
1) iteration is the content we want to fill in, and the format after the first % is the format we want to choose.
2)[+] is right alignment and '+' is a parameter we can fill in. The number represents the alignment width.
3)[-] is left alignment and '-' is a parameter we can fill in. The number represents the alignment width.
4) The m in [.m] is optional precision, indicating how many decimal places to retain after the decimal point.
5) The formatting character is the format we need to select. Its common types are string %s, decimal integer %d, single character %c, floating point number %f, hexadecimal number %x, and octal number %o ,character%%.
The understanding of the % operator may be relatively abstract. Let's learn about it through examples. What we commonly use is to output a floating point number, retaining 2 decimal places after the decimal point. The code is as follows:
>>>my_str=66666.66666>>>print('The format with 2 decimal places is: %.2f'%my_str) The format with 2 decimal places is: 66666.67
When retaining decimals, the rounding operation will be automatically performed. When using it, pay attention to its form. We can choose one or more to combine together to format the string. Above we use a formatting operation, the formatting part is '%.2f', and then use % at the end of the string to connect the corresponding string, which is directly equivalent to the corresponding content.
Let’s introduce the use of alignment again:
>>>foriinrange(5):...print('%-5d'%i,end='')#Left alignment...print('%5d'%i)#Right alignment...0011223344
5 is the width of the code alignment. We use left and right alignment. The first column is left aligned and the width is 5. Then i is output again without wrapping. The second output is right aligned, so the output is as shown above.
This is about the % operator. You can master this knowledge faster by practicing it yourself below.
The format() method provides more methods to format strings. Its basic syntax is to replace '%' with '{}' and ':'. Its syntax format is:
str.format()
Let's look at it through an example:
namea='Xiao Ming' nameb='Xiao Hua' print('{} is {}'s good friend'.format(namea,nameb))
This example is the simplest way to use it, but since this method was introduced later, it must have its advantages. Let’s introduce its specific method below.
In the placeholder the structure can be:
{[index[:[fill]align][width][#][.k][type]]}
Let’s look at this structure through the diagram:
The above example only contains simple '{}'. If we modify it, we can use the above structures. Let's explain one by one:
1) Index is the index position. We can give it a number to specify the index position of the object in the list. The index value starts from 0. If there is no index value, it is in order from left to right. Like the example above, if we add the index value:
namea='Xiao Ming' nameb='Xiao Hua' print('{1} is a good friend of {0}'.format(namea,nameb))
Then their positions were swapped.
2) ':' is a prerequisite for subsequent use. You must add a ':' when using subsequent operations.
3) fill can select a parameter as the number of characters for blank filling.
4) width allows you to choose a parameter to specify the length.
5) align can choose a parameter as the alignment method. The parameters are '>' for right alignment, '<' for left alignment, '=' for right alignment, but the symbol is placed on the far left, and '^' represents centering. These require Used with width.
Let's look at an example:
>>>my_str='dotcpp'>>>print('{:@>10}'.format(my_str))@@@@dotcpp
In this example we chose right alignment, then set the width to 10, and replaced whitespace characters with the @ symbol.
6)'#' is the base prefix.
7).k in k is an optional parameter, used to represent the number retained after the decimal point.
8) type can select parameters to specify the type. Commonly used ones include S to represent a string, D to represent a decimal integer, % to represent a percentage, and f or F to represent a floating point number.
Below we use an example to use the above methods.
my_str='dotcpp'#First define two strings my_string=123456.654321print('my_str:{1:!^20s}nmystring:{0:$^20.2f}'.format(my_string,my_str))
The output is:
my_str:!!!!!!dotcpp!!!!!!!mystring:$$$$$123456.65$$$$$$
For my_str, '1' is its index position, '!' replaces whitespace characters, '^' represents the center position, 20 is the width, and 's' is the string type.
For my_string, '0' is its index position, '$' replaces the empty packet character, '^' represents the center position, 20 bits wide, '.2' represents 2 decimal places after the decimal point, and 'f' represents floating point type.
That’s it for talking about strings. Strings are used very frequently, so everyone must practice and master it through exercises.