Check Palindrome String Recursively
Check if string is palindrome recursively.
AdvancedPhase 3 - Level 4: String-based RecursionExample 2 of 10
check-palindrome-string-recursively.py
1def is_palindrome(s):2 # Base case3 if len(s) <= 1:4 return True56 # Check first and last7 if s[0].lower() != s[-1].lower():8 return False910 # Recurse on middle11 return is_palindrome(s[1:-1])1213# Test14text = input("Enter a string: ")15if is_palindrome(text):16 print("Palindrome")17else:18 print("Not a palindrome")
Output
Enter a string: racecar Palindrome Enter a string: hello Not a palindrome
What's going on
Compare first and last, recurse on middle.
Key Concepts:
Base case: empty or single character
Check first == last
Recurse on s[1:-1] (middle part)
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum