split()
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — What is split()?
`split()` is a string method that breaks a string into a list of pieces. By default, it splits on spaces. Super useful when you have data stored as text!
team = "Aquachu Pyrochu Florachu"
team_list = team.split()
print(team_list)
print(type(team_list))Exercise 2 — How split() Works
When you call `split()` with no argument, it splits on spaces. Each word becomes a separate element in the list.
sentence = "Aquachu used Splash"
words = sentence.split()
print(words)
print(len(words))The original string is not modified — `split()` creates a new list.
Exercise 3 — split() Result
text = "Fire Water Plant"
result = text.split()
print(result[1])What is printed?
Hint : `split()` creates a list, and list indexing starts at 0.
Exercise 4 — Split on Spaces
Complete the code to split the move list and print the second move.
Hint : `split()` with no argument splits on spaces. The second element is at index 1.
Exercise 5 — Split with a Custom Separator
You can split on any character by passing it as an argument. Common separators: `,` `-` `/` `:`
data = "Aquachu,Fire,Level 5"
parts = data.split(",")
print(parts)
print(parts[0])Here we split on commas. The separator itself is removed from the results.
Exercise 6 — Split with Separator
Complete the code to extract the Codémon name from this save data.
Hint : Look at what character separates the data. The name is the first element.
Exercise 7 — split() with Separator
text = "Aquachu-Pyrochu-Florachu"
result = text.split("-")
print(len(result))What is printed?
Hint : Count the elements between the separators.
Exercise 8 — split() with maxsplit
You can limit how many splits are made by passing a second argument called maxsplit.
data = "Aquachu:Water:30:100"
parts = data.split(":", 2)
print(parts)
print(len(parts))With `maxsplit=2`, only the first 2 `:` are split. The rest stays in the last element.
Syntax
The syntax is `string.split(separator, maxsplit)`. This is useful when you only need the first few parts of a string.
Exercise 9 — Using maxsplit
Complete the code to split only on the first colon, so we can unpack the name and keep the rest together.
Hint : `maxsplit=1` means split only once, producing exactly 2 parts — perfect for unpacking into 2 variables.
Exercise 10 — split() on Missing Separator
What happens if the separator is not found in the string?
text = "Aquachu"
result = text.split(",")
print(result)Hint : If the separator isn't found, the whole string becomes a single-element list.
Exercise 11 — split() Results
Match each `split()` call with its result.
Exercise 12 — Parse Codémon Data
You receive Codémon data as a single string. Parse it! Given the variable `data`: 1. Split by `|` to get each Codémon 2. For each Codémon, split by `:` to get name, type, level 3. Print each one as: `"Aquachu (Water) - Level 30"`
data = "Aquachu:Water:30|Pyrochu:Fire:25|Florachu:Plant:28"
# 1. Split by | to get each Codemon
# 2. For each, split by : and print