Join a List to a String in Python

  • Post category:List

Converting a list to a string is a common operation in Python. In this blog post, we’ll explore different code examples that demonstrate how to join a list to form a string efficiently.

Example 1:

fruits = ['apple', 'banana', 'orange', 'kiwi']
fruits_string = ', '.join(fruits)
print(fruits_string)

Output: “apple, banana, orange, kiwi”

Example 2:

numbers = [1, 2, 3, 4, 5]
numbers_string = ' '.join(map(str, numbers))
print(numbers_string)

Output: “1 2 3 4 5”