python - 字符串作为元组返回
问题描述
我在 Python 中编写了一个函数,它返回基于 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))
输出:
('Joshua', 'Jackie', 'and', 'Marguerite', 'are awesome!')
期望的输出:
"Joshua, Jackie, and Marguerite are awesome!".
解决方案
这样做的原因是,在 python 中,如果你使用 , 来分隔一些值,它会被解释为一个元组。所以当你返回时,你返回的是一个元组,而不是一个字符串。您可以加入元组,也可以使用如下格式字符串。
return f'{test_first_TA}, {test_second_TA}, and {test_third_TA} are awesome!'
推荐阅读
- xml - xsd 模式中的常量键
- c# - 仅在超过-1/1 时如何对 Vector3.normalized 进行标准化?
- c# - 发布到 IIS ASP.Net Core 导致“值不能为空”错误
- entity-framework - 如何使用 where 语句在两个表之间创建 Map?
- laravel - Laravel Route 给出 Object not found 错误
- python - 处理高度不平衡数据的正确方法 - 二进制分类
- javascript - 如何拆分javascript函数
- php - 如何在php中创建一个嵌套数组
- sql - 如何在不遍历 SQLite3 的情况下从 SQL 表中获取最后一列的名称?
- python - 如何比较两个不同对象的列表并返回匹配项?