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]