Lesson 20
Splitting & Joining Text
split() cuts a string into a list. With no argument it splits on whitespace; with an argument it splits on that separator.
join() does the reverse. The separator comes first: ", ".join(items) glues the items together with a comma and a space.
Strings are immutable, so methods like replace() return a new string instead of changing the original.
words = "learn python today".split()
print(len(words))
print("-".join(words))Check your understanding
What does "a-b-c".split("-") return?
CHALLENGE
Split "red green blue" into words, then print the words joined with a comma and no spaces.
Practice →