Joe Joe - 7 months ago 16
Python Question

Print to stdout with space's between result?

I'm stuck with a little problem,
I want to print to stdout the results with spaces between them

x y

my code prints it
(x, y)
because I'm only returning them with comma between them

return x, y

What do I have to add before printing x and y?


Your function returns a tuple. What you see is the standard representation of tuples.

There are different solutions;

1) Assign to two variables and print:

x, y = function()
print x, y # in Python 3 use print(x, y)

2) In python 3 you can unpack:

res = function()

3) You can use format:

res = function()
print '{} {}'.format(res[0], res[1])
# In python 3 also with unpacking; print('{} {}'.format(*res))

Note that using format probably gives you the most flexibility w.r.t. how the output looks. For instance if the values are floating point numbers.