Module 4: Strings

Index, slice and transform text: string methods and the classic string programs every exam asks.

  1. Indexing and slicing: s[0] first, s[-1] last; s[a:b] includes a but not b; s[::-1] reverses; strings can't be changed in place, so build a new one.
  2. String methods: Methods return a new string (save it!); find gives -1 but index crashes; split β†’ list, "sep".join(list) β†’ string, partition β†’ 3-tuple.
  3. String programs for practicals: Every string program is: loop over the characters, test each with in or an is… method, update a counter or build a new string; return True only after the loop.
  4. Text puzzles: ciphers and anagrams: ord turns a character into its code and chr back; shift letters with (pos + k) % 26; anagrams have equal sorted() letters.

Ends with a 13-question checkpoint (80% passes) and a mini project: Password strength checker.