In Python, you can split a string into a list of substrings using split():
s.split() # splits on any whitespace (default)
s.split(delim) # splits on a specific delimiter
Here are some examples:
sentence = "hello world foo"
words = sentence.split()
print(words) # ["hello", "world", "foo"]
csv = "a,b,c"
parts = csv.split(",")
print(parts) # ["a", "b", "c"]
You can loop through the resulting list:
for word in words:
print(word)
Assignment
Follow the Coding Tutorial and split sentence into a list of words!
Hint
Use sentence.split() and assign the result to words.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
