版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zong596568821xp/article/details/83008634
1 %的方式
1.1 打印字符串
print ("My name is %s"%("zongxp"))
1.2 打印整数
print ("I am %d years old"%(25))
1.3 打印浮点数
print ("My height is %f m"%(1.75))
1.4 打印浮点数(指定保留小数点位数)
print ("My height is %.2f m"%(1.75))
1.5 指定占位符宽度
print ("Name:%10s Age:%8d Height:%8.2f"%("zongxp",25,1.75))
1.6 指定占位符宽度(左对齐)
print ("Name:%-10s Age:%-8d Height:%-8.2f"%("zongxp",25,1.75))
2 format的方式
利用format。这是官方推荐用的方式,%方式将可能在后面的版本被淘汰。
>>> print('{1},{0},{1}'.format('zongxp',25)) # 通过位置传递,相当方便,可以重复,可以换位置。
25,zongxp,25
>>> print('{name}: {age}'.format(age=25,name='zongxp')) # 通过关键字传递
zongxp: 25