Python Join List Elements with String

Document 对象参考手册Python3 Examples

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)

Code explanation:

  1. my_list = ['Hello', 'World', 'Python']: Define a list containing three string elements.
  2. 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.
  3. print(result): Print the joined string.

Output result:

Hello World Python

Document 对象参考手册Python3 Examples

Other extensions