Rocket has the crew list ready for the flight board: Rocket, Raven and Kai. He prints the list, and the board shows square brackets and quote marks.
"That looks like code, not a sign," he sighs. Raven tilts her head. "What do you notice? We want commas and spaces between the names, and nothing else."
Nova hums a little tune. "Would you like a hint? Split cuts a string into a list. Something else glues a list back into a string."
join makes one string from a list of strings.
First write the separator string. Then a dot, join, and the list in brackets.
crew = ["Rocket", "Raven", "Kai"]
print(crew)
print(", ".join(crew))
print("-".join(crew))
print("".join(["N", "O", "V", "A"])) | The screen shows |
|---|
| ['Rocket', 'Raven', 'Kai'] |
| Rocket, Raven, Kai |
| Rocket-Raven-Kai |
| NOVA |
parts = ["deck", "3", "clear"]
print(" ".join(parts))
print("/".join(parts)) days = ["Mon", "Tue", "Wed"]
print(" ".join(days))
print(", ".join(days))
print("".join(days))
print(" and ".join(days)) | Statement | True or false? |
|---|---|
| "".join(["a", "b"]) gives "ab" | ? |
| join puts a separator after the last piece. | ? |
| split and join are opposites. | ? |
| The separator comes after the dot. | ? |
Smooth joining! Tomorrow you will use split and join to decode the comms log.