Python Print List Elements Separated by Commas
In Python, we can usejoin()method to print list elements separated by commas.join()The method is a string method that joins the elements in a list into a string, inserting the specified delimiter between each element.
Example
my_list = ['apple', 'banana', 'cherry']
result = ', '.join(my_list)
print(result)
result = ', '.join(my_list)
print(result)
Code explanation:
my_list = ['apple', 'banana', 'cherry']: Define a list containing three string elements.result = ', '.join(my_list): Usejoin()method to join the elements in the list into a string, inserting commas and spaces between each element.print(result): Print the joined string.
Output result:
apple, banana, cherryOther Extensions
Python3 Examples