Python格式化输出

 1.打印字符串

 Python X
>>>print ("His name is %s"%("Aviad"))
His name is Aviad

 2.打印整数

 Python X
>>>print ("His is %d years old"%(25))
His is 25 years old

 3.打印浮点数

 Python X
>>>print ("His height is %f m"%(1.83))
His height is 1.830000 m

 4.打印浮点数(指定保留小数位数)

 Python X
>>>print ("His height is %.2f m"%(1.83))
His height is 1.83 m

 5.指定占位符宽度

 Python X
>>>print ("Name:%10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))
Name:     Aviad  Age:      25 Height:    1.83

 6.指定占位符宽度(左对齐)

 Python X
>>>print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))
Name:Aviad      Age:25       Height:1.83    

 7.指定占位符(只能用0作占位符?)

 Python X
>>>print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))
Name:Aviad    Age:00000025   Height:00001.83

 8.科学计数法

 Python X
>>>format(0.0015,'.2e')
'1.50e-03'
原文地址:https://www.cnblogs.com/yiven/p/9770898.html