Your name isn’t one thing to a computer — it’s a numbered row of characters. Once you see that, you can pull it apart.
The book’s tour of strings. The indexing section is the one to try by hand. Enrolled in our Runestone course? Open it from there so your progress counts.
Still in Programs. You’ve handled numbers; now text gets the same treatment. And the reason it works reaches back to Bits: a string is ASCII codes in a row (Unit 1), which is exactly why it has an order and a length.
That “count from zero” rule feels wrong for about a week and then becomes second nature. It is also one of the most reliable ways beginners lose a point, so we’re going to stare at it until it’s obvious.
A string is a sequence of characters — text, in quotes. Three things you can do with one:
Join them with +. This is concatenation: gluing
strings end to end. (You met the dark side of this in 2.4 — + on two strings
joins instead of adds.) It works the same in both languages:
first = "Ada" last = "Lovelace" full = first + " " + last print(full) # Ada Lovelace
first ← "Ada" last ← "Lovelace" full ← first + " " + last DISPLAY(full) // Ada Lovelace
Hover or tap a line to light its twin. Notice line 3 adds a
" " — without that space you’d get AdaLovelace.
Measure them with len(): the number of
characters in the string. len("Python") is 6. Reach into them with
indexing: word[0] is the first character, word[1] the
second, and so on. The index is the address; the character is what lives there.
word is
word[len(word) − 1] — or, Python’s shortcut, word[-1].
This “length is one past the last index” is the same 2n vs 2n−1
idea from Unit 1, wearing new clothes.
Type a word. Each character gets its address. Click any box to see the exact code that pulls that character out.
Try word[0] (first) and notice the last box is
len(word) − 1, never len(word) — asking for
word[len(word)] runs off the end and errors.
+. "cat" + "dog" is
"catdog".word[2] is
the character at index 2 — the third one.Five questions. Count carefully — the off-by-one is the whole game. Pick an answer for instant feedback.
“There are only two hard things in computer science,”
the old joke goes, “cache invalidation, and off-by-one errors.” Reading or writing
one position past the end of a sequence — asking for word[len(word)] —
is not just a beginner slip. In languages with fewer guardrails than Python, that exact mistake
is behind some of the most serious security holes ever found, because reading past the end can
leak whatever data happened to sit next to it in memory.
Python protects you: index too far and it stops with a clear error
instead of handing back a stranger’s data. That safety is a design choice with a real cost
(Python is slower) and a real benefit (a whole class of catastrophic bugs simply can’t
happen). You’ll meet the security side of this in Unit 7. For now: the boundary is
n−1, and it matters.
str() from the 2.5
lab).