Common reasons for formatting output using python

Source: Internet
Author: User
This article summarizes some simple and basic formats of output formatting. I will not talk about them here. let's take a look at the details. 1. print the string & gt; print & quot; Im % s & quot; % (& quot; jihite & quot ;) imjihite 2. print integer & gt; print & quot; Im % dyearsold & quot; % (17) Im17yearsold 3. print floating point & gt; & gt; print & quot; π % f & quot; % (3.1415926) π 3 This article summarizes some simple and basic output formatting formats, let's take a look at the detailed introduction.
I. print strings

>>> print "I'm %s" % ("jihite")I'm jihite


II. print the integer

>>> print "I'm %d years old" % (17)I'm 17 years old


3. print floating point numbers

>>> print "π=%f" % (3.1415926)π=3.141593


4. print the floating point number (specify the number of decimal points to be retained)

>>> print "π=%.3f" % (3.1415926)π=3.142


5. specify the placeholder width

>>> print "NAME:%8s AGE:%8d WEIGHT:%8.2f" % ("jihite", 17, 62.2)NAME: jihite AGE:   17 WEIGHT:  62.20


6. specify the placeholder width (left aligned)

>>> print "NAME:%-8s AGE:%-8d WEIGHT:%-8.2f" % ("jihite", 17, 62.2)NAME:jihite  AGE:17    WEIGHT:62.20


7. specify a placeholder (only 0 can be used as a placeholder)

>>> print "NAME:%-8s AGE:%08d WEIGHT:%08.2f" % ("jihite", 17, 62.2)NAME:jihite  AGE:00000017 WEIGHT:00062.20


8. Scientific notation

>>> format(0.0000023, '.2e')'2.30e-06'>>> format(0.23, '.2e')'2.30e-01'



The above is a detailed description of the common causes of formatting output using python. For more information, see other related articles in the first PHP community!

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.