Python Join List Elements with String
In Python, we can usejoin()method to join the string elements in the list.join()The method will concatenate all string elements in the list into a single string, and can insert a specified delimiter between elements.
Example
# Define a list containing strings
my_list = ['Hello', 'World', 'Python']
# Use the join() method to join the elements in the list, using a space as the delimiter
result = ' '.join(my_list)
# Print the result
print(result)
my_list = ['Hello', 'World', 'Python']
# Use the join() method to join the elements in the list, using a space as the delimiter
result = ' '.join(my_list)
# Print the result
print(result)
Code explanation:
my_list = ['Hello', 'World', 'Python']: Define a list containing three string elements.result = ' '.join(my_list): Usejoin()method to join the elements in the list into a string,' 'means inserting a space between elements as the delimiter.print(result): Print the joined string.
Output result:
Hello World PythonOther extensions
Python3 Examples