Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python打印以作为代码的一部分执行字符串文字_Python - Fatal编程技术网

python打印以作为代码的一部分执行字符串文字

python打印以作为代码的一部分执行字符串文字,python,Python,我正在使用python使JDBC连接的一些SQL查询更加模块化,并希望使用print语句执行部分字符串文本 fh = open("data.txt","w") print("42 is the answer, but what is the question?", file=fh) fh.close() 我知道我可以用它来写文本,但我想把打印输出保存在一个局部变量中,以便以后在其他语句中调用 fh = open("data.txt","w") print("42 is the answer,

我正在使用python使JDBC连接的一些SQL查询更加模块化,并希望使用print语句执行部分字符串文本

fh = open("data.txt","w")
print("42 is the answer, but what is the question?", file=fh)
fh.close()
我知道我可以用它来写文本,但我想把打印输出保存在一个局部变量中,以便以后在其他语句中调用

fh = open("data.txt","w")
print("42 is the answer, but what is the question?", file=fh)
fh.close()

这可能吗?

谢谢您编辑Wondercricket.@Flufylobster我不明白您的意思。把你的问题说清楚。
output = "42 is the answer, but what is the question?"
with open("data.txt","w") as fh:
    fh.write(output)