
Code
Split and Join
split chops a string into pieces wherever it finds a
separator. It gives you a list of the pieces. (You'll
master lists in the next course. For now, index them like
strings.)
route = "Emberreach>Moonwell>Lumen"
stops = route.split(">")
print(stops) # ['Emberreach', 'Moonwell', 'Lumen']
print(stops[0]) # Emberreach
print("a b c".split()) # ['a', 'b', 'c'] – no argument splits on spaces
join does the opposite. It glues pieces together, with the
string before the dot placed between each piece:
print(" -> ".join(stops)) # Emberreach -> Moonwell -> Lumen
A few more handy search tools:
route.find("Moon") # 11 (index where it starts, -1 if missing)
route.startswith("Ember") # True
route.endswith("Lumen") # True
route.count(">") # 2
Assignment
Starting from cargo:
- Set
itemstocargosplit on commas. - Set
first_itemto the first item initems. - Set
labelto the items joined with" | "between them. - Set
oil_atto the index where"oil"starts incargo(usefind). - Set
rope_firstto whethercargostarts with"rope".
Starting from cargo:
- Set
itemstocargosplit on commas. - Set
first_itemto the first item initems. - Set
labelto the items joined with" | "between them. - Set
oil_atto the index where"oil"starts incargo(usefind). - Set
rope_firstto whethercargostarts with"rope".
main.py Python sleeping
Press Run to try your code, then Check to test it.