Python string formatting code

  • 2020-04-02 09:49:19
  • OfStack

Format summary:

format describe format describe % % Percent mark % (extra % is escape)     % c Characters and their ASCII codes % s string % d Signed integer (decimal) The % u Unsigned integer (decimal) % o Unsigned integer (octal)     % x Unsigned integer (hexadecimal) % X Unsigned integer (hexadecimal uppercase character) % e Floating point number (scientific notation) % E Floating point number (scientific notation, E instead of E) % f Floating point number (with decimal symbol)     % g Floating point number (automatically selects %e or %f depending on the value) % G Floating point number (similar to %g, automatically selects %E or %f depending on the value) % p Pointer (memory address of a value printed in hexadecimal) % n The number of stored output characters is placed in the next variable in the parameter list

However, since everything can be converted to string in python, you can use '%s' for everything if you don't have any special requirements.
For example: '%s %s %s' % (1, 2.3, ['one', 'two', 'three'])
Its output is '1 2.3 ['one', 'two', 'three']', which is output according to the mark to the left of %.
Although the first and second values are not of type string, that's fine.
In this process, when the computer finds that the first value is not %s, it first calls the function of integer, converts the first value (1) to string, and then calls the STR () function to output.

The above is just the simplest form of the format tag, but it is a little more complicated:
Output length:
'% 6.2 f' % 1.235
In this form, a decimal of 6.2 appears before f, which means that the total length of the output is six characters with two decimal places.

There are more complicated ones:
Placeholder:
'% 06.2 f' % 1.235
The extra 0 in front of the 6 indicates that if the output number is less than 6 bits, the 0 will complement the 6 bits.
The output on this line is' 001.24', and you can see that the decimal also takes up a bit.

And the notation that looks something like 0 here is minus, plus. Where - indicates left alignment, and + indicates a + sign in front of a positive number, which is not added by default.

Example:
%ns: if the string variable s is less than n bits, several Spaces are printed before str1 is printed
%-ns: in contrast to the above, output the variable s first, and then fill in the blanks.

Other:
Dictionary format output:
'% (name) s: % (score) 06.1 f' % {' score: 9.5, 'name' : 'newsim'}
This form is used only if the output is dictionary. The (name) and (score) in parentheses correspond to the keys in the key-value pairs that follow.

Sometimes in the form of %6.2f, 6 and 2 can not be specified in advance, will be generated during the program running, so how to input, of course, can not use %%d.%df or %d.%d%f.
You can use the form %*.*f, which of course contains the two * values in the "group of values to output".
For example: '%*.*f' % (6, 2, 2.345) is equivalent to '%6.2f' % 2.345.
However, if you can't remember, or don't want to be so patient, you can completely replace it with %s, or use multiple +'s to construct similar output strings.


Related articles: