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:

  1. Set items to cargo split on commas.
  2. Set first_item to the first item in items.
  3. Set label to the items joined with " | " between them.
  4. Set oil_at to the index where "oil" starts in cargo (use find).
  5. Set rope_first to whether cargo starts with "rope".