ITPub博客

首页 > IT职业 > IT图书 > python中如何实现print函数格式化输出?

python中如何实现print函数格式化输出?

IT图书 作者:micromad 时间:2021-09-11 10:04:31 0 删除 编辑

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。    

一、print() 方法介绍

用于打印输出,是python中最常见的一个函数。

语法

print(*objects, sep=' ', end='n', file=sys.stdout)

二、数据格式化输出实例

1、格式化输出字符串

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

2、格式化输出整数

strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
print strHello
#输出果:the length of (Hello World) is 11

3、格式化输出浮点数

a = 3.1415926
print "%f" %a
输出为:
3.141593

4、格式化输出指定占位符宽度

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

以上就是小编总结四种使用python中print函数实现格式化输出的方式,希望能对你有所帮助哦~

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2763/viewspace-2831528/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    75
  • 访问量
    574