Every crew member needs a three-letter tag for the flight board. Rocket writes a loop to make them. The tags come out as OC, AV and AI.
"Those are only two letters," Raven says. "What do you notice about where each tag starts?"
Rocket looks again. "Oh! I started at index 1." Nova projects the word rocket with numbers under each letter. "Great discovery. Would you like a hint? Remember where Python starts counting."
This loop takes the first three letters of each name. Then it makes them capitals.
crew = ["rocket", "raven", "kai"]
for name in crew:
tag = name[0:3].upper()
print(tag) | The screen shows |
|---|
| ROC |
| RAV |
| KAI |
name[0:3] takes the first three letters. Then .upper() makes them capitals.
Rocket had written name[1:3]. That starts at the second letter and stops before index 3.
names = ["Rocket", "Kai", "Starfinder"]
for n in names:
if len(n) > 5:
print(n[0:5])
else:
print(n) words = ["galaxy", "moon", "comet"]
for w in words:
print(w[0:3].upper(), len(w)) | Statement | True or false? |
|---|---|
| "nova"[0:3].upper() is "NOV" | ? |
| len("moon") > 5 is True | ? |
| .upper() changes digits | ? |
| "Starfinder"[0:5] is "Starf" | ? |
Week five is done! You can count, slice, change and search strings. Next week, you will split strings apart and join them back together.