Overview
Python strings are immutable sequences with rich built-in methods for searching, splitting, and transforming.
Analogy
A string is a bead necklace: you can look at any bead by index, but cutting and restringing creates a new necklace.
Step-by-step
- s[i] accesses index i; s[a:b] slices.
- s.split(sep) splits by separator into a list.
- sep.join(lst) joins a list back into a string.
- s.find(sub), s.replace(old,new), s.strip(), s.upper(), s.lower().
Visual
'hello world'.split() → ['hello','world']; '-'.join(['a','b']) → 'a-b'
Common mistakes
- Concatenating strings in a loop (O(n²)); use ''.join(list) instead.
- Forgetting strings are immutable: s[0]='H' raises TypeError.
Practice questions
- Reverse words in a sentence: ' '.join(s.split()[::-1]).
- Count vowels in a string.
Time
O(n) for most operations
Space
O(n) for new strings