Ms. Whitby's Classroom logo Ms. Whitby's Classroom

Strings & Text Processing — Lesson 1: Read characters by position

New skill: String indexing and length
Previously learned: Units 01–06

Why this matters

Player names, QR labels, and message codes are strings. Programs often need to inspect just a few characters.

Step 1 — Explore working Python

Read the program below. Before running it, write down the result you expect.

label = 'T-BIRD'
print(len(label))
print(label[0])
print(label[-1])

Step 2 — Trace the code

  1. Run the example and note that the string has six characters.
  2. Trace index 0 to the first character, then trace index -1 to the final character.
  3. Notice that indexing reads a character but does not change the original string.

Step 3 — Check your understanding

For the text ROBOT, predict text[1] and len(text) before running.

Step 4 — Watch for this mistake

text[len(text)] goes one position beyond the last valid index. Use len(text) - 1 or -1.

Practice: Read characters by position

Complete edge_letters(text) to return the first and last characters joined into a two-character string. The input contains at least two characters.

Function contract: edge_letters(text)
Starter file: main.py

Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.

Hint 1 — Start Access the first character with index 0.
Hint 2 — Build A negative index can select the final character.
Hint 3 — Test Join the two selected characters with `+`.

Reflect

In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.

Optional reading

For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.