Converting a list to a string with commas is a common task in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to join a list of elements into a single string with commas efficiently.
fruits = ['apple', 'banana', 'kiwi'] fruits_string = ', '.join(fruits) print(fruits_string)
Output: apple, banana, kiwi
numbers = [1, 2, 3, 4, 5] numbers_string = ', '.join(str(num) for num in numbers) print(numbers_string)
Output: 1, 2, 3, 4, 5