Alex Apostolu
Alex Apostolu

Reputation: 19

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)

Upvotes: 1

Views: 7473

Answers (3)

J. Meijers
J. Meijers

Reputation: 989

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.

Upvotes: 1

yixing yan
yixing yan

Reputation: 183

tuple will print the round brackets

print(tuple(list_of_numbers))

Upvotes: 0

BohanZhang
BohanZhang

Reputation: 163

print(tuple(list_of_numbers))

or

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

Upvotes: 4

Related Questions