MASTER YOUR LOGIC BUILDING:Phase 3 - Level 4: String-based Recursion

Mastering phase 3 - level 4: string-based recursion concepts and implementation.

Phase 3 - Level 4: String-based Recursion

Why This Matters

A string is just a row of characters — like beads on a necklace. Recursion lets you look at one bead, then ask the same function to handle the rest of the necklace. Once that clicks, reverse, palindrome checks, and counting become the same pattern with different "what do I do with this bead?" steps.

What You'll Learn

  • The classic string-recursion pattern: process first char + recurse on the rest
  • How to reverse a string recursively
  • How to check a palindrome by comparing ends, then shrinking the middle
  • Your string-slicing toolkit for recursion

Your String Toolkit (Memorize These)

SliceMeaning
`s[0]`first character
`s[-1]`last character
`s[1:]`everything except the first
`s[:-1]`everything except the last
`s[1:-1]`everything except first and last

Almost every string recursion uses one of these.

The Core Pattern

  1. Base case — empty string or one character → answer is obvious
  2. Handle one character (usually the first, sometimes both ends)
  3. Recurse on the smaller string
  4. Combine your piece with the recursive result

Walkthrough: Reverse a String

Think: "The reverse of 'hello' is (reverse of 'ello') + 'h'."

def reverse_string(s):
    if len(s) <= 1:          # base case
        return s
    return reverse_string(s[1:]) + s[0]

Trace for `"cat"`:

  • reverse("cat") → reverse("at") + "c"
  • reverse("at") → reverse("t") + "a"
  • reverse("t") → "t"
  • Result: "t" + "a" + "c" → "tac"

Walkthrough: Palindrome Check

A palindrome reads the same forwards and backwards. Recursively:

  1. If 0 or 1 characters → True (tiny doll)
  2. If first ≠ last → False
  3. Otherwise check the middle the same way
def is_palindrome(s):
    if len(s) <= 1:
        return True
    if s[0].lower() != s[-1].lower():
        return False
    return is_palindrome(s[1:-1])

Tips

  • Always shrink the string (s[1:] or s[1:-1]) so you move toward the base case
  • For case-insensitive checks, compare .lower() on both sides
  • Strings are immutable — you build a new result; you don't edit the original in place

Common Mistakes

  • Forgetting the base case for empty/len == 1
  • Comparing characters but forgetting to recurse on the middle
  • Off-by-one slices that never reach the base case

Practice Focus

Use the hands-on examples below to reverse a string and check palindromes recursively. Say the pattern out loud: "Handle one character, trust the rest."

Hands-on Examples

Reverse String Recursively

def reverse_string(s):
    # Base case
    if len(s) <= 1:
        return s
    
    # Recursive case: reverse rest, then add first character
    return reverse_string(s[1:]) + s[0]

# Test
text = input("Enter a string: ")
result = reverse_string(text)
print(f"Reversed: {result}")

Base case: single or empty string returns itself. Recursive: reverse substring (s[1:]) and append first character (s[0]).