简体   繁体   中英

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!".

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!'

You can do it using join

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(' '.join(my_TAs(test_first_TA, test_second_TA, test_third_TA)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM