python - 从 Python3 写入 "table"

标签 python python-3.x

如何在一个文件的一行中同时写入多个字符串和多个变量输出来输出? 我知道 write() 只接受一个参数,但理想情况下这是我想要实现的目标:

write('Temperature is', X , 'and pressure is', Y)

结果将是一个表格。

你能帮忙吗?

最佳答案

write('Temperature is {0} and pressure is {1})'.format(X,Y))

如果你想更好地控制输出,你可以这样做:

X = 12.3
Y = 1.23
write('Temperature is {0:.1f} and pressure is {1:.2f})'.format(X,Y))
# writes 'Temperature is 12.3 and pressure is 1.2'

此处的文档和示例:http://docs.python.org/py3k/library/string.html

关于python - 从 Python3 写入 "table",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4129624/

相关文章:

python - django-haystack 自动完成功能无法正常工作

python - 使用 Python Matlibplot 和 im.show() 显示图像,为什么它们不同?

python - 在 Jupyter 中显示来自 Redis 订阅的流数据帧

python - Python 中一个非常大的整数的 math.pow 是错误的

python - 如何将 seaborn 图保存为 svg 或 png

python - 如何在 python 中显示文件中的西里尔文本?

python - 使用 HoverTool() 工具时仅显示一个工具提示

python - 使用请求选择和上传 pdf 文件时遇到问题

python - 无法删除结果之间的巨大空格

python-3.x - 使用 Python 与句子形式的数据关联规则