为什么 Python 用引号和逗号打印我的字符串?

Why does Python print my string with quotes and commas?

所以当我运行下面的代码时:

x = 4
y = 'the value', x, 'is truthy'
print(y)

我不明白为什么我的输出是:('the value', 4, 'is truthy')

例如,如果我直接打印这行代码:

print('the value', x, 'is truthy') 

这不是一个严重的问题,我能够使用格式化功能来避免问题 V

def truthy_or_falsy2(arg):
    if arg: 
        return f'The value {arg} is truthy'
    
    return 'The value {arg} is falsy'

感谢您的建议!

当你写 y = 'the value', x, 'is truthy' 时,你将 y 设置为一个元组,因为你将它设置为以逗号分隔的多个值。对于它是一个简单的字符串,您可以使用连接:
y = 'the value '+ str(x) + ' is truthy'
或字符串格式:
y= 'The value {0} is truthy'.format(x)
当您将它直接写入 print('the value', x, 'is truthy') 之类的打印语句时不会出现此问题的原因是函数参数的工作方式:每个逗号分隔值作为另一个参数传递给函数,打印语句只是打印每个论证出来。因此,当您这样写时,每个逗号分隔的项目都被视为一个不同的项目并打印,而不是全部放在一个元组中,然后当您将它分配给变量时将其作为一个整体打印出来 y = 'the value', x, 'is truthy'