This article summarizes the Python format string method, share to everyone for your reference. The specific analysis is as follows:
The Python string format method is described in the form of an example:
* Define Width
The Python code is as follows:
>>> '%*s '% (5, ' some ') ' some '
-Align Left
The Python code is as follows:
>>> '%-*s '% (5, ' some ') ' some '
Floating-point decimals with a minimum width of 6 and a 2-bit precision, with insufficient digits to fill the space before
The Python code is as follows:
>>> '%6.2f '%8.123 ' 8.12 '
Dictionary form to display a plus sign before a positive number, 0 when the digits are not enough
The Python code is as follows:
>>> '% (name) s =% (num) +06.2f '%{' name ': ' A ', ' num ': 8.123} ' a = +08.12 '
Displays 0 (' 0 ') before the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on ' 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 '
Hopefully this article will help you with Python programming.