← Back to course
Code Crew G4 / Week 05 / Friday
5/6
Week 05 Β· Strings up close

Friday

Make crew tags
// Letter by letter
⏱ about 15 min

Friday: Make crew tags

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."

Mix the tools

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.

SLICE AND SHOUT
  • Read the question.
  • Tap your answer.
What is "orbit"[0:3].upper()?
What is "orbit"[1:3]?

Short names for small screens

names = ["Rocket", "Kai", "Starfinder"]
for n in names:
    if len(n) > 5:
        print(n[0:5])
    else:
        print(n)
PUT THE SCREEN LINES IN ORDER FOR THE PROGRAM ABOVE
  • ?Rocke
  • ?Kai
  • ?Starf
WHY THIS EXERCISEReal programs trim long text so it fits on small screens.
EDGE CASE
  • Read the question.
  • Tap your answer.
Suppose the list also holds "Raven". Which path does that round take?
What does that round show?
At the computer
This is never required.
If a grown-up has set up Python from python.org, try the crew tags loop in IDLE.
Add more names to the list.

Practice lab: tags and lengths

words = ["galaxy", "moon", "comet"]
for w in words:
    print(w[0:3].upper(), len(w))
What number shows on line 1?
What number shows on line 2?
What number shows on line 3?
TAG CHECK
  • Read the question.
  • Tap your answer.
What tag shows on line 1?
Which slice gives the last three letters of "comet"?
What is "kai"[0:3].upper()?
StatementTrue or false?
"nova"[0:3].upper() is "NOV"?
len("moon") > 5 is True?
.upper() changes digits?
"Starfinder"[0:5] is "Starf"?
WHY THIS EXERCISEMixing tools works best one step at a time, left to right.
What is len("Rocket"[1:4])?
WHY THIS EXERCISE"Rocket"[1:4] is "ock", which has 3 characters.
Try it
Write five space words on scraps of paper.
Under each one, write its tag and its length, like the loop would.
Check the longest word last.

Week five is done! You can count, slice, change and search strings. Next week, you will split strings apart and join them back together.

← Thursday