21

Phase 5 - Category 3: Reversing & Palindromes

Chapter 21 • Intermediate

75 min

Phase 5 - Category 3: Reversing & Palindromes

Introduction

Master string reversal techniques and palindrome checking. Learn to reverse entire strings, individual words, and word order.

Key Concepts

String Reversal

  • Full Reverse: Reverse entire string
  • Word Reverse: Reverse each word separately
  • Word Order Reverse: Reverse order of words
  • Selective Reverse: Reverse only certain parts

Palindrome Operations

  • Check Palindrome: String reads same forwards and backwards
  • Case-Insensitive: Ignore case differences
  • Ignore Spaces: Remove spaces before checking
  • Reverse Check: Check if two strings are reverses

Reversal Techniques

Full String Reverse

python.js
reversed_str = text[::-1]

Word-by-Word Reverse

python.js
words = text.split()
reversed_words = [word[::-1] for word in words]

Word Order Reverse

python.js
words = text.split()
reversed_order = ' '.join(reversed(words))

Palindrome Checking

Simple Check

python.js
if text == text[::-1]:
    print("Palindrome")

Case-Insensitive

python.js
if text.lower() == text.lower()[::-1]:
    print("Palindrome")

Problem-Solving Approach

  1. Determine Type: Full reverse, word reverse, or word order?
  2. Use Slicing: [::-1] for reversal
  3. Handle Words: Split, reverse, join
  4. Normalize: Lowercase, remove spaces for comparison

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.