python--formatted output, placeholder, format ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.