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
- Determine Type: Full reverse, word reverse, or word order?
- Use Slicing:
[::-1]for reversal - Handle Words: Split, reverse, join
- 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.
Related Tutorials
🔗Related Content
- 💻
Phase 5 - Practice Problems
Practice Phase 5 concepts with hands-on coding problems
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with assessment questions
- ➡️
Phase 6 - Get Started
Continue to Phase 6 after mastering Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum