Word Games: Palindromes & Flips
Lesson 4 of 5 · View course roadmap
Learn the idea
Strings hide a whole toy box. The star trick is slicing with a negative step: word[::-1] reads the string backwards.
A palindrome reads the same both ways ("racecar"). The test writes itself — but real words have capital letters, so normalise first:
word.lower()— level the playing fieldword[::-1]— the reversed stringclean == clean[::-1]— the palindrome test
This normalise-then-compare pattern is everywhere in real code: case-insensitive logins, search matching, de-duplicating names. You're learning it with toys; you'll use it at work.
Where you'll use this
Normalise-then-compare powers case-insensitive logins, search engines, duplicate detection and spell checkers. DNA analysis uses the same reversal tricks on genetic sequences.
Common mistakes
- Comparing without normalising case first — 'Level' != 'level' to Python.
- Confusing word.reverse() (doesn't exist for strings) with the slice word[::-1].
- Testing the original word instead of the cleaned one after lowering it — clean, then use the clean value everywhere.
Pro tip
For phrase palindromes ('Never odd or even'), strip non-letters first: clean = "".join(ch for ch in text.lower() if ch.isalpha()). One comprehension makes the test bulletproof.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +30 XP
For each word in ["Level", "python", "Racecar"], print word -> palindrome! if it reads the same backwards (ignoring case), otherwise word -> not a palindrome. Keep the word's original capitalisation in the output.
Level -> palindrome! python -> not a palindrome Racecar -> palindrome!
clean = word.lower(), then compare clean == clean[::-1]. Print with f"{word} -> ...".
words = ["Level", "python", "Racecar"]
for word in words:
clean = word.lower()
if clean == clean[::-1]:
print(f"{word} -> palindrome!")
else:
print(f"{word} -> not a palindrome")
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.