python common formatted output summary

  • 2020-05-17 05:52:45
  • OfStack

This article summarizes some simple and basic output formatting forms. Without further discussion, let's take a look at the details.

1. Print a string


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

2. Print integers


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

3. Print floating point Numbers


>>> print " PI. =%f" % (3.1415926)
 PI. =3.141593

4. Print floating point Numbers (specify reserved decimal places)


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

5. Specify a placeholder width


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

6. Specify 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 use 0 as a placeholder)


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

8. Scientific counting


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

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: