简体   繁体   English

如何在python中更改打印语句的输出格式?

[英]How do I change the output format of a print statement in python?

I wrote a code like below 我写了下面的代码

A = print("apple")

And, I want to change the data type of A to string, so I wrote the following code. 并且,我想将A的数据类型更改为字符串,因此我编写了以下代码。

A = str(print"apple")
type(A)

But, the answer was "None". 但是,答案是“无”。 In this What should I do in this situation? 在这种情况下我该怎么办?

Actually what I'm doing right now is to write something like the output to a text file. 实际上,我现在正在做的是将类似输出的内容写入文本文件。

openFp = open("solution.txt","w")
p2 = str(print("I_B : ",I_B))
openFp.writelines(p2+"\n")
openFp.close()

Entering this code will always result in 'None'. 输入此代码将始终显示“无”。

That's impossible. 这不可能。

Because print just simply prints the parameter, then returns nothing, so you force to print the print(...) or make as variable it's gonna be None, so nothing you can do. 因为print只是简单地打印参数,然后什么都不返回,所以您强制打印print(...)或将其设为变量,因此它将为None,因此您无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM