Every hatch on Beacon Station has a tiny screen that shows a call sign. Rocket types "Nova 7" for the new drone bay. Then he wonders how much room it takes.
"Six," Raven says. Rocket counts again. "N, o, v, a, seven. That is five." Raven smiles. "What do you notice between the a and the seven?"
Nova peeks at the screen and giggles. "That is my name! Would you like a hint? A space is a character too. Python counts it."
A string is a row of characters. A character is one letter, digit, space or symbol.
len counts the characters in a string, just like it counts items in a list.
Each character has an index. The first index is 0, and -1 is the last one.
call = "Nova 7" print(len(call)) print(call[0]) print(call[-1])
| The screen shows |
|---|
| 6 |
| N |
| 7 |
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Character | N | o | v | a | (space) | 7 |
word = "ROCKET" print(word[3]) print(word[-1])
| Statement | True or false? |
|---|---|
| len("go now") is 6 | ? |
| len("") is 0 | ? |
| "Raven"[0] is "a" | ? |
| "Raven"[-1] is "n" | ? |
tag = "Deck 9B" print(len(tag)) print(tag[0]) print(tag[5]) print(tag[-1])
| Statement | True or false? |
|---|---|
| len("Hi there") is 8 | ? |
| "Kai"[1] is "a" | ? |
| "Kai"[3] is "i" | ? |
| A space is not a character. | ? |
Good counting! Tomorrow you will cut a string into pieces with a slice.