MASTER YOUR LOGIC BUILDING:Phase 5 - Category 3: Reversing & Palindromes

Mastering phase 5 - category 3: reversing & palindromes concepts and implementation.

Reversing & Palindromes

Why This Matters

Ever typed a word backwards for fun — like reading "racecar" in a mirror and noticing it looks the same? Reversing strings and checking palindromes are classic programming puzzles that teach you to think about order. Many real tasks — undo buttons, spell-checkers, DNA sequence analysis — involve reading text forward, backward, or comparing both directions.

What You'll Learn

  • How to reverse an entire string with one line of slicing
  • How to reverse individual words or the order of words
  • How to check if a string is a palindrome
  • When to normalize text (lowercase, remove spaces) before comparing

The Magic Slice: [::-1]

In Python, text[::-1] reads the string backwards. That's it — your best friend for reversal problems.

word = "Python"
print(word[::-1])   # "nohtyP"

Think of it as walking from the last character to the first, picking up each one.

Three Types of "Reverse" (Don't Mix Them Up!)

1. Reverse the whole string

"Hello World"[::-1]   # "dlroW olleH"

2. Reverse each word, keep word order

text = "Hello World"
words = text.split()
result = ' '.join(word[::-1] for word in words)
print(result)   # "olleH dlroW"

3. Reverse word order, keep words as-is

text = "Hello World"
words = text.split()
result = ' '.join(reversed(words))
print(result)   # "World Hello"

Read the problem carefully — "reverse the string" and "reverse each word" are different!

Step-by-Step: Check a Palindrome

A palindrome reads the same forwards and backwards: "madam", "level", "A man a plan a canal Panama" (ignoring spaces).

  1. Normalize — remove spaces, convert to lowercase
  2. Reverse — create the backwards version
  3. Compare — are they equal?
text = "Racecar"
clean = text.replace(" ", "").lower()

if clean == clean[::-1]:
    print("Palindrome!")
else:
    print("Not a palindrome")

Two Pointers (Another Way to Check)

Instead of building a reversed copy, compare characters from both ends moving inward:

clean = "level"
left = 0
right = len(clean) - 1
is_palindrome = True

while left < right:
    if clean[left] != clean[right]:
        is_palindrome = False
        break
    left += 1
    right -= 1

Both approaches work — slicing is shorter; two pointers is a great exercise in logic.

Common Mistakes & Tips

  • Forgetting to normalize — "Racecar" fails a simple check unless you use .lower()
  • Reversing word order when you meant to reverse letters — re-read the question!
  • Including spaces in comparison — "a ba" is not a palindrome as-is, but "aba" is
  • Tip: Test with short examples first: "", "a", "aa", "ab" — edge cases reveal bugs quickly

Practice Focus

  • Reverse a string the user types
  • Reverse each word in a sentence separately
  • Check if a word is a palindrome (case-insensitive)
  • Reverse the order of words: "I love Python" → "Python love I"

Palindrome and reversal problems look tricky at first, but once [::-1] feels natural, you'll solve them in minutes!

Hands-on Examples

Reverse Each Word

# Take string input
text = input("Enter a string: ")

# Split into words
words = text.split()

# Reverse each word
reversed_words = []
for word in words:
    reversed_words.append(word[::-1])

# Join back
result = ' '.join(reversed_words)
print(f"Reversed words: {result}")

Split string into words, reverse each word using slicing [::-1], then join words back with spaces.