Start learning free

LESSON 68 OF 673 · STRINGS I

String Split in Python

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check