Logic Building
def is_palindrome(s):
# Base case
if len(s) <= 1:
return True
# Check first and last
if s[0].lower() != s[-1].lower():
return False
# Recurse on middle
return is_palindrome(s[1:-1])
# Test
text = input("Enter a string: ")
if is_palindrome(text):
print("Palindrome")
else:
print("Not a palindrome")Output
Enter a string: racecar Palindrome Enter a string: hello Not a palindrome
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)