簡體   English   中英

字符串作為元組返回

[英]String is returned as a Tuple

我在 Python 中寫了一個 function ,它返回應該基於 3 個參數的簡單字符串,但它返回一個元組而不是一個字符串。

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

所需的 output:

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

原因是在 python 中,如果使用,來分隔某些值,則將其解釋為元組。 所以當你返回時,你返回的是一個元組,而不是一個字符串。 您可以加入元組,也可以使用如下格式字符串。

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

您可以使用加入來做到這一點

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)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM