栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Python的同一行上打印变量和字符串?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Python的同一行上打印变量和字符串?

使用

,
分隔字符串和变量,同时打印:

print("If there was a birth every 7 seconds, there would be: ", births, "births")

,
in print功能将项目分隔为一个空格:

>>> print("foo", "bar", "spam")foo bar spam

或更好地使用字符串格式:

print("If there was a birth every 7 seconds, there would be: {} births".format(births))

字符串格式化功能更强大,它还允许您执行其他一些操作,例如填充,填充,对齐,宽度,设置精度等。

>>> print("{:d} {:03d} {:>20f}".format(1, 2, 1.1))1 002  1.100000  ^^^  0's padded to 2

演示:

>>> births = 4>>> print("If there was a birth every 7 seconds, there would be: ", births, "births")If there was a birth every 7 seconds, there would be:  4 births# formatting>>> print("If there was a birth every 7 seconds, there would be: {} births".format(births))If there was a birth every 7 seconds, there would be: 4 births


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637889.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号