>> print "I'm %d years old" % (17)I'm 17 years old 3. Print floating point numbers >>> print "π=%f" % (3.1415926)π=3"/> >> print "I'm %d years old" % (17)I'm 17 years old 3. Print floating point numbers >>> print "π=%f" % (3.1415926)π=3">

Home >Backend Development >Python Tutorial >Common reasons for using python to format output

Common reasons for using python to format output

高洛峰
高洛峰Original
2017-03-24 17:44:521399browse

This article summarizes some simple and basic output formatting forms. I won’t say much below, let’s take a look at the detailed introduction.
1. Print string

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


2. Print integer

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


3. Print Floating point number

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


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

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


5. Specify the occupancy 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 the placeholder width placeholder (only 0 can be used as 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 the detailed content of Common reasons for using python to format output. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn