【问题标题】:How can I take off the parenthesis when I output a tuple in python?在python中输出元组时如何去掉括号?
【发布时间】:2021-06-15 17:20:52
【问题描述】:
exam_st_date = (11, 12, 2014)
print(f'The examination will start from {exam_st_date}')

我得到的输出是考试将从(11,12,2014) 开始。我怎样才能摆脱这些括号?

【问题讨论】:

标签: python


【解决方案1】:

我认为您不明白为什么要打印括号。这是因为 (11,12,2014) 是一个元组。我的建议是使用正确的 datetime 变量和 datetime.strftime 或者您可以简单地使用 str.join 格式化元组

print(f'The examination will start from {",".join(exam_st_date)}')

【讨论】:

    【解决方案2】:

    您可以将元组 exam_st_date 转换为字符串并删除其第一个和最后一个字符:

    print(f'The examination will start from {str(exam_st_date)[1:-1]}')
    

    【讨论】:

      【解决方案3】:

      这样做: 考试日期=(2014 年 11 月 12 日) print(f'考试将从{exam_st_date[0],exam_st_date[1],exam_st_date[2]}开始考试')

      【讨论】:

        【解决方案4】:
        print(f'The examination will start from {str(exam_st_date).strip("()"))}')
        

        【讨论】:

        • 非常感谢。你的回答真的很简单很有帮助
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-13
        • 1970-01-01
        • 2019-03-22
        • 1970-01-01
        • 2012-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多