pythonstringprintingformattingcustom-formatting

Print all items in a list with a delimiter


Given a Python list, what is the preferred way to print it with comma delimiter/separator, and no intervening spaces, and no trailing comma at the end, after the last element:

I tried:

a = [1, 2, 3]
for element in a:
  print str(element) + ",", # old Python 2 syntax

output
1,2,3,
desired
1,2,3

Solution

  • >>> ','.join(map(str,a))
    '1,2,3'