Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Print List Elements Separated by Commas

Python Print List Elements Separated by Commas

Document Object Reference Manual Python3 Examples

In Python, we can use join() The method prints the elements of a list separated by commas.join() The method is a string method that joins the elements of a list into a string, inserting the specified separator between each element.

Example

my_list

=

[

'apple'

,

'banana'

,

'cherry'

]

result

=

', '

.

join

(

my_list

)

print

(

result

)

Code analysis:

  1. my_list = ['apple', 'banana', 'cherry']: Define a list containing three string elements.
  2. result = ', '.join(my_list): When using join() The method joins the elements of a list into a string, inserting a comma and a space between each element.
  3. print(result): Print the joined string.

Output result:

apple, banana, cherry

Document Object Reference Manual Python3 Examples