Search code examples
pythonstringfunctionreturntuples

String is returned as a Tuple


I wrote a function in Python which returns should simple string based on 3 parameters, but it returns a tuple instead of a string.

def my_TAs(test_first_TA, test_second_TA, test_third_TA):

    return test_first_TA, test_second_TA, "and", test_third_TA, "are awesome!."

test_first_TA = "Joshua"

test_second_TA = "Jackie"

test_third_TA = "Marguerite"

print(my_TAs(test_first_TA, test_second_TA, test_third_TA))

Output:

('Joshua', 'Jackie', 'and', 'Marguerite', 'are awesome!')

Desired output:

"Joshua, Jackie, and Marguerite are awesome!".

Solution

  • The reason for this is that in python, if you use , to separate some values, it is interpreted as a tuple. So when you return, you are returning a tuple, not a string. You could either join the tuple, or use format strings like below.

    return f'{test_first_TA}, {test_second_TA}, and {test_third_TA} are awesome!'