【发布时间】: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
exam_st_date = (11, 12, 2014)
print(f'The examination will start from {exam_st_date}')
我得到的输出是考试将从(11,12,2014) 开始。我怎样才能摆脱这些括号?
【问题讨论】:
标签: python
我认为您不明白为什么要打印括号。这是因为 (11,12,2014) 是一个元组。我的建议是使用正确的 datetime 变量和 datetime.strftime 或者您可以简单地使用 str.join 格式化元组
print(f'The examination will start from {",".join(exam_st_date)}')
【讨论】:
您可以将元组 exam_st_date 转换为字符串并删除其第一个和最后一个字符:
print(f'The examination will start from {str(exam_st_date)[1:-1]}')
【讨论】:
这样做: 考试日期=(2014 年 11 月 12 日) print(f'考试将从{exam_st_date[0],exam_st_date[1],exam_st_date[2]}开始考试')
【讨论】:
print(f'The examination will start from {str(exam_st_date).strip("()"))}')
【讨论】: