Python format string example summary
- 2020-04-02 14:10:04
- OfStack
This article summarizes python's method of formatting strings as an example and shares it with you for your reference. Specific analysis is as follows:
The python string formatting method is expressed in the form of an example:
* define width
The Python code is as follows:
>>>'%*s' %(5,'some')
' some'
- the left
The Python code is as follows:
>>>'%-*s' %(5,'some')
'some '
A two - bit precision floating point decimal with a minimum width of 6
The Python code is as follows:
>>>'%6.2f' %8.123
' 8.12'
In dictionary form, a plus sign can be displayed in front of a positive number
The Python code is as follows:
>>>'%(name)s = %(num)+06.2f' %{'name':'a','num':8.123}
'a = +08.12'
Displays zero ('0') before octal number and '0x' or '0x' before hexadecimal (depending on whether 'x' or 'x' is used)
The Python code is as follows:
>>>'dec: %d/oct: %#o/hex: %#X' % (123,123,123)
'dec: 123/oct: 0173/hex: 0X7B'
Scientific enumeration
The Python code is as follows:
>>>'%e' % 1234.567890
'1.234568e+03'
I hope this article has helped you with your Python programming.