Andres Imery
Andres Imery

Reputation: 3

print statement comma acting weird in python

When I code:

x = 4
y = 10

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

I get as a result:

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

Instead of:

X is 4 and Y is 10

Why is this happening? Please help.

Upvotes: 0

Views: 344

Answers (2)

user1767754
user1767754

Reputation: 25094

Just do it the modern way with string formatting

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

Or old school

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

or use

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

Upvotes: 0

John Gordon
John Gordon

Reputation: 33310

You must be using python 2, where print is a keyword instead of a function. It is interpreting the comma-separated items inside the parentheses as a tuple, and so it is printing the tuple.

Just remove the parentheses and it will work as you expect.

Upvotes: 2

Related Questions