在Python 3.6中使用括号打印列表

时间:2017-09-06 22:41:28

标签: python python-3.x list tuples

这个问题与其他问题不同,因为我试图打印带有圆括号的列表,而不是方括号。

例如;我有这个清单:

list_of_numbers = [1, 2, 3, 4, 5]

当您打印出列表时,您会得到:

[1, 2, 3, 4, 5]

我希望打印版看起来像这样:

(1, 2, 3, 4, 5)

3 个答案:

答案 0 :(得分:2)

print(tuple(list_of_numbers))

print('(%s)' % ', '.join([str(i) for i in list_of_numbers]))

答案 1 :(得分:1)

list_of_number_strings = [str(number) for number in list_of_numbers]
list_string = "({})".format(", ".join(list_of_number_strings))
print(list_string)

应该做的伎俩

list_of_number_strings使用简单的列表解析通过将list_of_numbers中的每个元素转换为字符串来创建字符串列表。 然后我们使用简单的字符串格式和连接来创建我们想要打印的字符串。

答案 2 :(得分:0)

元组将打印圆括号

print(tuple(list_of_numbers))
相关问题