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
sentenceshould be"banana chocolate computer umbrella guitar" - Use the function
join() - Do not change the first line of the code
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules