Lecture Notes — Lecture 10: Strings Under the Lens

Companion to the lecture plan and the worked examples. Read the Definitions before class, the Explanation after it; the practice questions and the lecture quiz close the loop.

Definitions

Explanation

Immutable means rebuild. s.upper() does not change s; it returns a new string. Building output = accumulate in a list, join at the end (cs-039's template fill).

Normalise before you compare. cs-040: 'Dormitory' vs 'dirty room!' agree only after lowercasing, removing non-letters, and sorting characters. The ladder: case → strip noise → order. Skipping a rung gives wrong answers that look like logic bugs.

Slices as positional thought. cs-038's reversal family: s[::-1] reverses; word reversal needs split → reverse each → join. Each is one line because the problem is positional, not conditional.

Parse, don't crawl. cs-039 uses {name} placeholders and split/strip; hand-managed index arithmetic is where string bugs live. Rule: if you are counting characters by hand, look for a split/join formulation first.

Classify per character. cs-037's username audit is a per-character classifier plus an adjacency rule ('no two underscores in a row' - track the previous character). State that carries one character of history is a streak pattern from lecture 9 in disguise.

Examples

The lecture's worked examples live in examples.md; the case studies this lecture builds on are worked in class from the projector. The case sequence with difficulties:

Common misconceptions

Summary and key takeaways

  1. Normalise (case, noise, order) before any comparison.
  2. Strings are immutable: build new ones, accumulate-then-join.
  3. Prefer split/join over hand-managed indexes.
  4. One character of history (previous char) powers adjacency rules.

Practice questions

Where this leads

Next lecture: Lists and Slicing. The quiz below checks this lecture's essentials before we build on them.