1Understand
The idea
Strings are immutable: every + builds a new string, so building one character by character in a loop is O(n²). Collect pieces in a list and "".join(...) them once. Slicing (s[i:j], s[::-1]), split, strip and the is... methods cover most parsing; ord/chr turn letters into numbers when you need arithmetic on them.
Intuition
Strings in Python cannot be changed in place. Every s + ch allocates a new string, so building character by character in a loop is O(n²). Collect pieces in a list and finish with "".join(parts) once.
Slicing is your parsing tool: s[i:j] is half-open, s[::-1] reverses, and negative indices count from the end. split, strip, and the is... family handle most interview parsing; ord/chr turn letters into numbers when you need arithmetic on characters.
Case and whitespace are edge cases more often than people expect: normalise early (lower, strip) if the problem ignores them.
What you will be able to do
- Slice and index strings
- Split, strip and join
- Build strings with a list and join, not +=
2See it
Walkthrough
Join instead of +=
Build "abc" from characters, list + join is O(n), not O(n²).
1 / 4
Collect pieces in a list. Appending a character is O(1) amortized.
Indexing and slicing are O(length of the slice). A loop that concatenates strings is O(n²); list + join is O(n). Extra space is O(n) for the pieces. Prefer one pass that collects, then one join.
Read the walkthrough as text
Reverse words in a sentence. Given s = " hello world ", return "world hello" (trim, collapse spaces, reverse word order).
parts = s.split(), splits on any whitespace and drops empties, so you get["hello", "world"].- Reverse the list:
parts[::-1]→["world", "hello"]. return " ".join(parts)→"world hello".
Building with out = "" then out += word + " " in a loop works for tiny inputs but is the habit to break; join is clearer and linear.
3Recognise it
Kinds of problems (4)
Same pattern, different surfaces. Each row is a problem shape you should recognise, not only the first walkthrough example.
Slicing and reversing
Example. Check palindrome without extra space, or take every second character.
How. s[i:j:step] is the workhorse; s[::-1] reverses; bounds clamp safely on short strings.
Parsing with split and strip
Example. Parse "a, b, c" into clean tokens.
How. split on a delimiter, then strip() each piece (or split and filter empties).
Character checks (isalnum, ord/chr)
Example. Keep only letters/digits or shift a character by k in the alphabet.
How. str.isalnum(), isdigit(), etc. for classification; ord/chr map between code points and one-char strings.
Building output with join
Example. Concatenate a list of words with spaces between.
How. Collect strings in a list and "\n".join(parts), one allocation, not += in a loop.
Common mistakes
Failure modes the tutor watches for. Open the Common mistake tab in the walkthrough to watch one fail live.
Concatenating strings in a loop
Why it happens. s += piece looks like the obvious way to build text character by character.
How it fails. result = ""; for ch in parts: result += ch, O(n²) copies on large inputs and TLE.
Fix. Append to a list of chunks and "".join(parts) once, or use io.StringIO for very long builds.
Trying to modify a string in place
Why it happens. Strings look like lists, so s[i] = "x" feels legal.
How it fails. s[0] = "h" on "hello" raises TypeError; in-place reversal needs a list or new string slices.
Fix. Build a new string (slice concat, join, or list → join) instead of mutating characters.
Ignoring case sensitivity
Why it happens. Test cases often use mixed case while the mental model assumes exact match.
How it fails. "Aa" vs "aa" compared with == returns False; palindrome checks fail on one capital letter.
Fix. Normalize once: s.lower(), or casefold() when the problem is case-insensitive.
4Check yourself
Concept check
A short quiz on this lesson before you practise. Optional, and it does not gate coding.
Ready to practise?
Mastery comes from solving. The lesson is optional context.