Unit 2 · Programming 1 · Lesson 2.6

Strings I: Text Is a Row of Characters

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.

Big Idea 3 — Algorithms & ProgrammingCounting from zero

Before this lesson — read on Runestone

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.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

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.

2

The Hook

P Y T H O N To you that’s a word. To the machine it’s six characters in a fixed order, each with an address. The first character lives at address 0 — not 1.

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.

3

The Idea

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:

Python

first = "Ada"
last = "Lovelace"
full = first + " " + last
print(full)   # Ada Lovelace

AP Pseudocode

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.

A string of length n has characters at indexes 0 through n−1. So the last character of 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.
The 0-vs-1 heads-up. Python counts characters (and lists) from 0. AP Pseudocode counts lists from 1. That mismatch is a genuine trap, but it lives with lists, which arrive in Unit 3 — we’ll drill it properly there. For today: in Python, first means zero.
4

Try It — The Index Ruler

Type a word. Each character gets its address. Click any box to see the exact code that pulls that character out.

word =
len(word) = 6  ·  click a box to index it

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.

5

Vocabulary

string Programs
A sequence of characters treated as a single value, written in quotes. Underneath, it’s the characters’ codes in order (Unit 1’s ASCII).
concatenation Programs
Joining strings end to end with +. "cat" + "dog" is "catdog".
index Programs
The position of a character in a string, starting at 0 in Python. word[2] is the character at index 2 — the third one.
6

Check

Five questions. Count carefully — the off-by-one is the whole game. Pick an answer for instant feedback.

0 of 5 answered
7

Impact Check

The most expensive off-by-one

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

8

Connections

Came from Unit 1 — a string is ASCII codes in a row. Its order and length are that row made visible.
Next in 2.7 — string methods: change case, search for a substring, and build clean output with f-strings (finally fixing the clumsy str() from the 2.5 lab).
Returns in Unit 3 — lists are indexed just like strings, and that’s where the Python-0 vs Pseudocode-1 mismatch gets its own drill.
Returns on the exam — length vs last index is a classic MCQ. A length-n sequence ends at index n−1, every time.
← 2.5 Lab: Calculator Day