Python Print List Elements Separated by Commas
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
)
(
result
)
Code analysis:
my_list = ['apple', 'banana', 'cherry']: Define a list containing three string elements.result = ', '.join(my_list): When usingjoin()The method joins the elements of a list into a string, inserting a comma and a space between each element.print(result): Print the joined string.
Output result:
apple, banana, cherry
Python3 Examples