Reputation: 17
a=str(input("Enter string: "))
b=str(input("Enter another: "))
def switch(x, y):
x, y = y, x
return x, y
print (switch(a, b))
output is for example: ('there', 'hello') I want to remove parathenses and ''
Upvotes: 0
Views: 1155
Reputation: 133899
The return value of switch
is a tuple of 2 items (a pair), which is passed to the print
function as a single argument. And print
converts each of its arguments to a string with implicit str
, and the ('', '')
come from the string representation of this tuple.
What you want is to pass each item in the pair separately.
As this is Python 3, you just need to add one character:
print(*switch(a, b))
The *
means "pass the elements of the following iterable as separate positional arguments", so it is shorthand (in this case) for
value = switch(a, b)
print(value[0], value[1])
Print normally prints the values separated by a single space. If you want another separator, for example ,
, you can use the sep
keyword argument:
print(*switch(a, b), sep=', ')
Finally, the str()
in your example seem unnecessary.
Upvotes: 1
Reputation: 8600
Assuming you want to keep the output of your function the same (a tuple), you can use use join
to print the tuple separated by a space:
a = input("Enter string: ")
b = input("Enter another: ")
def switch(x, y):
return y, x
print(' '.join(switch(a, b)))
Small note: I changed the method to just be return y, x
, since the other two lines in the method did not seem needed in this case :)
Upvotes: 1