打印语句逗号在 python 中表现得很奇怪

print statement comma acting weird in python

当我编码时:

x = 4
y = 10

print("X is", x, "and Y is", y)

我得到的结果是:

('X is', 4, 'and Y is', 10)

而不是:

X is 4 and Y is 10

为什么会这样?请帮忙。

用现代的方式格式化字符串

print "x is {0} and y is {1}".format(x, y)

还是老派

print "x is " + str(x) + " and y is " + str(y)

或使用

print("X is %d and Y is %d" % (x, y))

您必须使用 python 2,其中 print 关键字 而不是函数。它将括号内以逗号分隔的项目解释为一个元组,因此它正在打印该元组。

只需删除括号,它就会如您所愿地工作。