Python string formatting symbol
Format character conversion method
% C is converted to a character (ASCII value, or a string of one length)
% Ra converts strings with the repr () function first.
% S String Conversion using the str () function
% D/% I to a signed decimal number
% Ub to unsigned decimal number
% Ob to unsigned octal number
% Xb/% Xb (Unsigned) to Unsigned hexadecimal number (x/X indicates the size of the converted hexadecimal character
Lowercase)
% E/% E into scientific notation (e/E control output e/E)
% F/% F is converted to a floating point number (the fractional part is naturally truncated)
% G/% G % e and % f/% E and % F
% Output %
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `
Auxiliary instructions for formatting Operators
Symbol function
* Define the width or decimal point Precision
-Left alignment
+ Display the plus sign (+) before a positive number)
<Sp> A space is displayed before a positive number.
# Display zero ('0') before the octal value, and '0x 'or '0x' before the hexadecimal value (depending on
Use 'X' or 'X ')
Fill '0' in front of the number displayed by 0 instead of the default Space
% 'Output a single' %'
(Var) ing variable (Dictionary parameter)
M. n m is the minimum total width displayed, and n is the number of digits after the decimal point (if available)
The following are some examples of using format strings:
Hexadecimal output:
>>> "% X" % 108
'6c'
>>>
>>> "% X" % 108
'6c'
>>>
>>> "% # X" % 108
'0x6c'
>>>
>>> "% # X" % 108
'0x6c'
Floating Point Number and scientific notation form output:
>>>
>>> '% F' % 1234.567890
'1970. 123'
>>>
>>> '%. 2f' % 1234.567890
'1970. 57'
>>>
>>> '% E' % 1234.567890
'1. 234568E + 03'
>>>
>>> '% E' % 1234.567890
'1. 234568e + 03'
>>>
>>> '% G' % 1234.567890
'1970. 57'
>>>
>>> '% G' % 1234.567890
'1970. 57'
>>>
>>> "% E" % (111111111111111111l)
'1. 1111e + 21'
Integer and string output:
>>> "% + D" % 4
'+ 4'
>>>
>>> "% + D" %-4
'-4'
>>>
>>> "We are at % d %" % 100
'We are at 100'
>>>
>>> 'Your host is: % s' % 'Earth'
'Your host is: global'
>>>
>>> 'Host: % s/tPort: % d' % ('mars', 80)
'Host: mars Port: 80'
>>>
>>> Num = 123
>>> 'Dec: % d/oct: % # o/hex: % # x' % (num, num, num)
'Dec: 123/oct: 0173/hex: 0X7B'
>>>
>>> "MM/DD/YY = % 02d/% 02d/% d" % (2, 15, 67)
'Mm/DD/YY = 02/15/67'
>>>
>>> W, p = 'web', 'page'
>>> 'HTTP: // xxx. yyy. zzz/% s/%s.html '% (w, p)
'Http: // xxx. yyy. zzz/Web/page.html'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> From string import Template
>>> S = Template ('there are $ {howation }$ {lang} Quotation symbols ')
>>>
>>> Print s. substitute (lang = 'python', howute = 3)
There are 3 Python Quotation Symbols