Strings and Character Processing

Lesson 4 of 20

Two pointers patrol racecar from both ends, then a frequency table counts every vowel in beautiful — one scan, no fuss.

The big picture

Text as a sequence

A string is an immutable sequence of characters: indexable, iterable, sliceable — but never editable in place.

Immutable

s[0] = "X" raises TypeError. Every “change” — upper(), strip(), + — builds a brand-new string object.

Index and slice

s[0] is the first char, s[-1] the last, s[::-1] the whole string reversed. len(s) is O(1).

Iterate directly

for ch in s hands you characters one by one — no index bookkeeping needed for most scans.

Membership

ch in "aeiou" is a clean membership test; "cat" in "catalogue" finds substrings too.

F-strings

f"{name} has {len(name)} letters" — {} slots run real Python expressions, right inside the text.