The launch board shows codes like GATE4-0900. The gate is at the front, and the time is at the back. Rocket needs to pull out just the time for the schedule.
"I will copy it letter by letter," he says. Raven shakes her head. "What do you notice? The time is always in the same spots."
Nova draws two glowing brackets around the last four digits. "Would you like a hint? Python can cut out a piece of a string. Just say where to start and stop."
A slice cuts a piece out of a string. Write the start index, a colon, then the stop index.
The slice includes the start. It stops just before the stop index.
word = "ROCKET" print(word[0:3]) print(word[3:6]) print(word[1:4])
| The screen shows |
|---|
| ROC |
| KET |
| OCK |
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Character | R | O | C | K | E | T |
place = "STATION" print(place[0:4]) print(place[4:7])
code = "GATE4-0900" print(code[6:10])
ship = "COMETRUN" print(ship[0:5]) print(ship[5:8]) print(ship[2:6])
| Statement | True or false? |
|---|---|
| "PLANET"[0:3] is "PLA" | ? |
| "PLANET"[3:6] is "NET" | ? |
| "PLANET"[1:3] is "PLA" | ? |
| A slice includes its stop index. | ? |
Sharp slicing! Tomorrow you will make strings shout and whisper with upper and lower.