Placeholder
Common placeholders |
Describe |
%s |
String |
%d |
Decimal integer |
%o |
Octal |
%x |
Hexadecimal |
%f |
Floating point number |
>>>Print('%s'%'Hello World')#string OutputHello World>>>Print('%20s'%'Hello World')#right-aligned, take 20 bits, not enough to fill the positionHello World>>>Print('%-20s'%'Hello World')#left-justified, take 20 digits, not enough to complementHello World>>>Print('%.2s'%'Hello World')#take 2-bitHe>>>Print('%10.2s'%'Hello World')#right-aligned, take 2-bitHe>>>Print('%-10.2s'%'Hello World')#left-Justified, take 2-bitHe>>>Print('%d Yuan'% 10) 10 USD>>>Print('%f'% 1.11)#6 decimal places are reserved by default1.110000>>>Print('%.1f'% 1.11)#take 1 decimal places1.1
Format ()
The format () function is more powerful relative to the basic formatted output using the '% ' method.
>>>Print('{} {}'. Format ('Hello',' World'))#with no fieldsHello World>>>Print('{0} {1}'. Format ('Hello',' World'))#with labelHello World>>>Print('{0} {1} {0}'. Format ('Hello',' World'))#Scramble OrderHello World Hello>>>Print('{1} {1} {0}'. Format ('Hello',' World') World Hello>>>Print('{A} {Tom} {a}'. Format (tom='Hello', a=' World'))#with KeywordsWorld Hello World
python--formatted output, placeholder, format ()