Home > Software engineering >  How can I print the number as elements of a list without the quotes and square brackets should be th
How can I print the number as elements of a list without the quotes and square brackets should be th

Time:12-03

The result should have square brackets enclosing the elements of list which are numbers , these numbers should not be enclosed into quotes.

i tried to do so with split function and for loop but was not able to get my desired result. i am expecting the answer.

CodePudding user response:

You can unpack all list elements into the print() function to print all values individually, separated by an empty space per default (that you can override using the sep argument). For example, the expression print(*my_list) prints the elements in my_list, empty space separated, without the enclosing square brackets and without the separating commas!

CodePudding user response:

You can use the join() method to print the list as a string, with the square brackets and commas between the elements, but without the quotation marks:

my_list = [1, 2, 3]
print('[{}]'.format(', '.join(str(x) for x in my_list)))

# Output: [1, 2, 3]
  • Related