0

I have this function

def one():
    item1 = "one"
    item2 = "two"
    result = item1, item2
    return  result

print(one())

the output of this funciotn is in format tuple, like this

('one', 'two')

What can I do if I need the output not in tuple, so in the following format

one, two

Can someone help? Thanks

gho
  • 171
  • 1
  • 8

3 Answers3

1

You can unpack it and specify a custom separator:

print(*one(), sep=', ')
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
1

Use str.join():

print(', '.join(one()))

Or in Python 3 you can simply use print() like this:

print(*one(), sep=', ')

which also works in Python 2 if you import print_function like this at the top of your file:

from __future__ import print_function
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • How can I call it in a nother funtion? – gho Apr 24 '16 at 10:37
  • for example: for v, vv in *one(), sep=', ': – gho Apr 24 '16 at 10:39
  • @gho: If you want a string then use `str.join()` as shown, and then pass that to the second function. e.g. `function2(', '.join(one()))`.Otherwise simply iterate over the returned tuple: `for item in one(): print(item)`. – mhawke Apr 24 '16 at 10:40
  • I will not make print, I only use it as input for another function – gho Apr 24 '16 at 10:44
  • @gho: did you read the answer and comments? Use `str.join()`, e.g. `another_function(', '.join(one()))` – mhawke Apr 24 '16 at 10:45
  • When I call this: for v, vv in ', '.join(one(item)): print(vv), I get this error: TypeError: sequence item 1: expected str instance, set found – gho Apr 24 '16 at 10:49
  • @gho: why are you doing that? Your function `one()` returns a tuple. You can unpack the tuple into separate variables, if that's what you are hoping to do: `item1, item2 = one()` will set `item1` to the string `one`, and `item2` to `two`. – mhawke Apr 24 '16 at 10:58
  • With my function I get this error: ValueError: too many values to unpack (expected 2) – gho Apr 24 '16 at 11:02
  • @gho: then the function that you posted in your question is different from the one that you are testing with. – mhawke Apr 24 '16 at 11:28
0

Here you go:

def one():
    item1 = "one"
    item2 = "two"
    result = item1 + ", " + item2
    return  result

print(one())
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97