Common reasons for using python to format output

高洛峰
Release: 2017-03-24 17:44:52
Original
1316 people have browsed it

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
Copy after login


2. Print integer

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


3. Print Floating point number

>>> print "π=%f" % (3.1415926)
π=3.141593
Copy after login


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

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


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
Copy after login


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
Copy after login


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
Copy after login


8. Scientific notation

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



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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!