Replacement
Position parameters
>>> ' {0} love {1}. {2} '. Format (' I ', ' junjie ', ' com ')
' I Love junjie.com '
Keyword parameters
>>> ' {A} love {b}. {c} '. Format (a= ' I ', b= ' Junjie ', c= ' com ')
' I Love junjie.com '
Positional parameters plus keyword parameter positional parameters must precede the keyword argument
>>> ' {0} love {b}. {c} '. Format (' I ', b= ' Junjie ', c= ' com ')
' I Love junjie.com '
>>> ' {A} love {b}. {0} '. Format (a= ' I ', b= ' junjie ', ' com ')
syntaxerror:positional argument follows keyword argument
Printing brackets
>>> ' {{0}} '. Format (' Do not print ')
' {0} '
Formatting symbols
>>> ' {0:.1f}{1} '. Format (27.68, ' GB '): Represents the beginning of a formatted symbol in a replacement field. 1 Rounding retain one decimal place f fixed number
' 27.7GB '
String formatting symbolic meaning
Symbol |
Description |
%c |
formatting characters and their ASCII code |
%s |
formatting strings |
%d |
formatting integers |
%o |
Formatting an unsigned octal number |
%x |
formatting unsigned hexadecimal numbers |
%x |
Format unsigned hexadecimal number (uppercase) |
%f |
Format floating-point numbers to specify the precision after a decimal point |
%e |
Format floating-point numbers with scientific notation |
%E |
function with%e, format floating-point numbers with scientific notation |
%g |
Use%f or%e depending on the size of the value |
%G |
function with%g, depending on the size of the value, use%f or%E |
Formatting operator Auxiliary commands
|
Description |
M.n |
M is the smallest total width displayed, and n is the number of digits after the decimal point |
- |
Use to align Left |
+ |
Display a plus sign before a positive number (+ |
# |
Display ' 0o ' before octal number, ' 0x ' or ' 0X ' before hexadecimal number |
0 |
The displayed number is preceded by a fill of ' 0 ' instead of a space |
Python's escape character and its meaning
Symbol |
Description |
\‘ |
Single quotation marks |
\" |
Double quotes |
\a |
Issue System ring tones |
\b |
Backspace |
\ n |
Line break |
\ t |
|
Python--8 formatting