繁体   English   中英

如何从返回中删除引号和括号

[英]How to remove quotes and parentheses from return

我必须在 python 中创建一个明确使用返回 function 的 function。 每次打印时,output 都会显示引号和括号,我得到停靠标记,想知道如何删除它们。

我的代码是:

def person(first_name, last_name, birth_year, hometown):                    
    info = (first_name + " " + last_name,str(birth_year),hometown)  
    return info

a = person('Lebron', 'James', 1984, 'Ohio')  

print(a)  

OUTPUT:

('Lebron James', '1984', 'Ohio')  

我需要 Output 是:

Lebron James, 1984, Ohio

您可以使用逗号空格字符串连接元素:

def person(first_name, last_name, birth_year, hometown):
    info = (first_name + " " + last_name,str(birth_year),hometown)
    return ', '.join(info)

a = person('Lebron', 'James', 1984, 'Ohio')

print(a)

Output 按要求

def person(first_name, last_name, birth_year, hometown):
    info = (first_name + " " + last_name,str(birth_year),hometown)
    return f"{info[0]}, {info[1]}, {info[2]}"

a = person('Lebron', 'James', 1984, 'Ohio')

print(a)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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