Python Print List Elements Separated by Commas

Document 对象参考手册Python3 Examples

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)

Code explanation:

  1. my_list = ['apple', 'banana', 'cherry']: Define a list containing three string elements.
  2. result = ', '.join(my_list): Usejoin()method to join the elements in the list into a string, inserting commas and spaces between each element.
  3. print(result): Print the joined string.

Output result:

apple, banana, cherry

Document 对象参考手册Python3 Examples

Other Extensions