Python格式化输出%s和%d

时间:2022-06-13 15:07:21

本文介绍了Python格式化输出%s和%d的实例案例。分享给大家供大家参考,具体如下:

python print格式化输出

1. 打印字符串

print ("His name is %s"%("Aviad"))

效果:

Python格式化输出%s和%d

2.打印整数

print ("He is %d years old"%(25))

效果:

Python格式化输出%s和%d

3.打印浮点数

print ("His height is %f m"%(1.83))

效果:

Python格式化输出%s和%d

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

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

效果:

Python格式化输出%s和%d

5.指定占位符宽度

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

效果:

Python格式化输出%s和%d

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

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

效果:

Python格式化输出%s和%d

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

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

效果:

Python格式化输出%s和%d

8.科学计数法

format(0.0015,'.2e')

效果:

Python格式化输出%s和%d

原文链接:https://blog.csdn.net/u013216667/article/details/51316971