Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Word Games: Palindromes & Flips

12 min 30 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 4 of 5 · View course roadmap
Step 1

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 field
  • word[::-1] — the reversed string
  • clean == 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.

Step 2

Try it yourself

Blank · autosaved

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”.

PYexample.py
+ Enter to run
Output appears here…
Step 3

Pass the challenge +30 XP

Blank · autosaved

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.

Target output
Level -> palindrome!
python -> not a palindrome
Racecar -> palindrome!
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. What does "python"[::-1] evaluate to?
2. Why call .lower() before the palindrome test?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path