The example of this article summarizes the Python format string method, shared for everyone to reference. The specific analysis is as follows:
The Python string formatting method is expressed as an example:
* Define Width
The Python code is as follows:
>>> '%*s ' (5, ' some ')
' some '
-Left alignment
The Python code is as follows:
>>> '%-*s ' (5, ' some ')
' some '
A floating-point decimal with a minimum width of 6, and a space before the number of digits is insufficient
The Python code is as follows:
>>> '%6.2f '%8.123
' 8.12 '
A dictionary form that shows a plus sign before a positive number, and a 0 before the digits are not enough
The Python code is as follows:
>>> '% (name) s =% (num) +06.2f '%{' name ': ' A ', ' num ': 8.123}
' A = +08.12 '
Display 0 (' 0 ') before the octal number, showing ' 0x ' or ' 0X ' before hexadecimal (depending on the ' x ' or ' x ')
The Python code is as follows:
>>> ' Dec:%d/oct:% #o/hex:% #X '% (123,123,123)
' dec:123/oct:0173/hex:0x7b '
Scientific counting method
The Python code is as follows:
>>> '%e '% 1234.567890
' 1.234568e+03 '
I hope this article will help you with your Python programming.