Python3 格式化输出
Python3 格式化输出
今天用字符串功能的时候,我突然忘记了格式化输出的方式X﹏X。所以赶紧恶补一下。
1.打印字符串
print("My name is %s" %("Alfred.Xue"))
#输出效果:
My name is Alfred.Xue
2.打印整数
print("I am %d years old." %(520))
#输出效果:
I am 520 years old.
3.打印浮点数
print ("His height is %f m" %(5.210))
#输出效果:
His height is 5.210000 m
4.打印浮点数(指定保留两位小数)
print ("His height is %.2f m" %(5.210))
#输出效果:
His height is 5.21 m
5.指定占位符宽度
print ("Dark%10s for%8d to%8.2f" %("zzy",520,13.141))
#输出效果:
Dark zzy for 520 to 13.14
6.指定占位符宽度(左对齐)
print ("Dark%-10s for%-8d to%-8.2f" %("zzy",520,13.141))
#输出效果:
Darkzzy for520 to13.14
7.指定占位符(继承了C语言的部分特性)
print ("Name:%-10s Age:%08d Height:%08.2f"%("Alfred",25,1.70))
#输出效果:
Darkzzy for00000520 to00013.14
8.科学计数法
format(0.00520,'.2e')
#输出效果:
'5.20e-03'
目前只会这些,更细的格式化输出我以后用到再更(/ω\)
大道五十,天衍四九,人遁其一!