简体   繁体   中英

Printing lists with brackets in Python 3.6

This question is different from others because I am trying to print lists that have round brackets, not the square ones.

For example; I have this list:

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

When you print out the list, you get this:

[1, 2, 3, 4, 5]

I want the printed version to look something like this:

(1, 2, 3, 4, 5)
print(tuple(list_of_numbers))

要么

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

Should do the trick

The list_of_number_strings uses a simple list comprehension create a list of strings by casting every element in list_of_numbers to a string. Then we use simple string formatting and a join to create the string we want to print.

元组将打印圆括号

print(tuple(list_of_numbers))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM