Lesson 12 of 26 · about 16 min
Strings up close
A string is a sequence of characters you can index, slice, search and transform. Strings never change; methods give you new ones.
🧭 What you need first: Decomposition and abstraction
Imagine a row of numbered lockers at a gym, one letter in each. To get a letter, you don’t say “the third one.” You say “locker 2,” because, for reasons programmers love, numbering starts at 0.
Want several letters in a row? Say “from locker 2 up to locker 5” and you get lockers 2, 3 and 4. (The “up to” doesn’t include the last one, just like range.)
And here’s the twist: these lockers are welded shut. You can look inside, but you can’t swap a letter. To “change” a word, Python builds a brand-new row of lockers.
Another way: floors in a building
In many countries the street level is floor 0, and the one above it is floor 1. Python counts characters the same way: the first character is at position 0. The index means “how many steps from the start.”
Another way: a ruler
Think of the index as the mark before each letter on a ruler. word[2:5] means “cut at mark 2 and at mark 5”, and you get what’s between the cuts. That’s why the stop isn’t included.
Index and slice by hand
Click letters to see their index. Move the sliders to build slices. Try a step of -1 with start at the end to reverse the word. Type your own word too.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Blue = index from the front · pink = negative index from the back
word[1:4] → "RAY"
The slice starts at start and stops before stop. A negative step walks backwards.
The real words
- A string is a sequence of characters.
len("hello")is5. - An index picks one character. Positive indexes count from the front starting at 0; negative indexes count from the back starting at -1. Asking for an index that doesn’t exist raises an
IndexError. - A slice
s[start:stop:step]makes a new string. Leave out start to begin at the front, leave out stop to go to the end.s[::-1]reverses. - Strings are immutable:
s[0] = "J"is an error. Instead, build a new string:s = "J" + s[1:]. - Loop over characters:
for ch in s:. Check membership:"cat" in "concatenate"isTrue.
Most useful methods (each returns something new; s itself never changes):
| Method | Example | Result |
|---|---|---|
.upper() .lower() |
"Hi".upper() |
"HI" |
.strip() |
" hi ".strip() |
"hi" |
.replace(old, new) |
"cat".replace("c", "b") |
"bat" |
.find(x) |
"banana".find("n") |
2 (or -1 if missing) |
.count(x) |
"banana".count("a") |
3 |
.split() |
"a b c".split() |
["a", "b", "c"] |
.join(list) |
"-".join(["a", "b"]) |
"a-b" |
.startswith(x) .endswith(x) |
"photo.jpg".endswith(".jpg") |
True |
Strings compare alphabetically by character codes, so "apple" < "banana" is True, but every capital letter comes before every lowercase one (remember A = 65, a = 97 from F01).
Three string tools
Write initials(full_name) → 'Ada Lovelace' gives 'AL'. Write count_vowels(text) → counts a, e, i, o, u (upper or lower case). Write is_palindrome(word) → True if it reads the same backwards, ignoring case.
A secret code (Caesar cipher)
Julius Caesar hid messages by shifting every letter 3 places: A→D, B→E… X→A. Remember ord("A") is 65 and chr(65) is "A": the letter-number agreement from F01.
Write caesar(text, shift) that shifts each capital letter by shift places, wrapping Z around to A. Leave spaces and other characters unchanged.
word = "PYTHON". What is word[1:4]?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?