【问题标题】:Why is my Python program printing parentheses and quotes where I don't expect them?为什么我的 Python 程序在我不期望的地方打印括号和引号?
【发布时间】:2021-05-17 15:58:01
【问题描述】:

我得到了这种奇怪的输出

我的代码是:

friends_name = ['Yashwanth','Koushik','Lalith','Narahari']
message = f"Hello buddy",friends_name[0]
print(message)
message = f'Hello buddy',friends_name[1]
print(message)

我在输出中得到括号和引号:

('Hello buddy', 'Yashwanth')
('Hello buddy', 'Koushik')

【问题讨论】:

    标签: python-3.x string list f-string


    【解决方案1】:

    这是因为您正在打印一个元组

    变量将是一个元组,因为有一个值f'Hello buddy'friends_name[1]

    如果您想在一条消息中打印它们

    message = f'Hello buddy {friends_name[1]}'
    

    【讨论】:

      【解决方案2】:

      这段代码:message = f"Hello buddy ,friends_name[0] 给你一个元组,它和message = (f"Hello buddy ,friends_name[0]) 一样。

      您可能想要的是插值,换句话说: message = f"Hello buddy {friends_name[0]}"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-26
        • 2017-08-12
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2021-08-27
        • 1970-01-01
        相关资源
        最近更新 更多