Editor settings

General settings

Save settings

Learn to program with Python

Combine a list of strings into one string #

We can combine a list of strings into a single string using the join() method, which is the opposite of the method split() we just learned about. This method takes a sequence (e.g., a list) of strings and concatenates them using the specified separator.

For example, here's how you can combine a list of words into a string with a space in between each word:

words = ['Hello', 'world', '!']
sentence = ' '.join(words)
print(sentence)

Exercise #

Use the method join() to combine a list of words words into a new variable sentence with a single space between each word.

Tests #

  • Variable sentence should be "banana chocolate computer umbrella guitar"
  • Use the function join()
  • Do not change the first line of the code
Output will be displayed here