Python3 formatted output% s &% d like

1. Print string

print("My name is %s" %("Alfred.Xue"))
#输出效果:
My name is Alfred.Xue

2. Print integer

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
print("I am %d years old." %(25))
#输出效果:
I am 25 years old.

3. Print a float

print ("His height is %f m"%(1.70))
#输出效果:
His height is 1.700000 m

4. Print floats (designated two decimal places)

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
print ("His height is %.2f m"%(1.70))
#输出效果:
His height is 1.70 m

The specified width placeholder

print ("Name:%10s Age:%8d Height:%8.2f"%("Alfred",25,1.70))
#输出效果:
Name:    Alfred Age:      25 Height:    1.70

6. Specify placeholder width (left)

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Alfred",25,1.70))
#输出效果:
Name:Alfred     Age:25       Height:1.70

7. Specify a placeholder (0 can only be used when a placeholder?)

print ("Name:%-10s Age:%08d Height:%08.2f"%("Alfred",25,1.70))
#输出效果:
Name:Alfred     Age:00000025 Height:00001.70

8. scientific notation

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
format(0.0026,'.2e')
#输出效果:
'2.60e-03'

Formatting code string:
Here Insert Picture Description

Published 706 original articles · won praise 728 · views 1 million +

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/103937186