[英]Embed variable using sys.stdout.write in Python
I can embed variables using the print statement in python in this way 我可以用这种方式在python中使用print语句嵌入变量
i=10
print "Value is %s" % (i)
Output 产量
Value is 10
but doing this 但这样做
i=10
sys.stdout.write ("Value is %s") % (i)
gives me the following error 给我以下错误
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
Can I embed variables using sys.stdout.write instead of print? 我可以使用sys.stdout.write而不是print来嵌入变量吗?
You got the parentheses wrong. 你的括号错了。 Should be
应该
i=10
sys.stdout.write("Value is %s" % i)
The %
operator takes a string and a tuple (or a single object) as arguments. %
运算符将字符串和元组(或单个对象)作为参数。 You tried to apply the operator to the return value of sys.stdout.write()
, which is None. 您试图将运算符应用于
sys.stdout.write()
的返回值,即None。 You need to apply it to the string before it is passed to sys.stdout.write()
. 在将字符串传递给
sys.stdout.write()
之前,需要将其应用于字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.