Beginner Python Exercises

Check a Palindrome in Python

"Never odd or even" reads the same backwards. Clean the text, reverse it with slicing and compare.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Normalizing text with lower() and filtering
  • Reversing with [::-1]
  • Comparing the two versions
"Never odd or even"
  1. 1"neveroddoreven"Lowercase, drop spaces
  2. 2cleaned[::-1]Reverse it
  3. 3"neveroddoreven"Same text → True

Clean, reverse, compare

The problem

Word games, data validation and many interview questions ask whether text reads the same in both directions. A plain comparison fails on sentences like "Never odd or even" because of capital letters and spaces, so the text must be cleaned first.

Think of it like this

Checking a palindrome is like holding a word up to a mirror after removing the decorations. First you strip off capital letters, spaces and punctuation, then you compare the word with its reflection. If they match, it is a palindrome.

From a mirror to code
character.lower()🔡 Make every letter the same size
if character.isalnum()🧹 Remove spaces and punctuation
normalized[::-1]🪞 The reflection, read backwards
normalized == normalized[::-1]✅ Compare with the mirror image
Clean first, then reverse, then compare.
In one sentence

A palindrome is text that reads the same forward and backward once differences such as capital letters, spaces and punctuation are ignored.

Key points

  1. character.lower() turns every character into lowercase, so "N" and "n" count as the same letter.
  2. if character.isalnum() keeps only letters and digits and drops spaces.
  3. "".join(...) glues the kept characters back into one string, normalized.
  4. normalized[::-1] is the same string backwards; comparing it with normalized gives True.

When should you use it?

  • Word games and puzzles.
  • Practicing string cleaning before comparing text, which real programs need everywhere.
  • Interview questions about strings and two-pointer techniques.

When not to use it

  • Very long texts where building a reversed copy wastes memory: compare characters from both ends instead.
  • Languages where characters combine (some accents and emoji): simple reversal can split them.

Example code

Run code →
main.py
text = "Never odd or even"
normalized = "".join(character.lower() for character in text if character.isalnum())

print(normalized == normalized[::-1])
Get a Run button for this example

Result of running the code

True

Code explanation

The normalized string keeps only letters and numbers. Slicing with a step of -1 produces the reversed value for comparison.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Comparing before cleaning

Capital letters and spaces make real palindromes fail.

Problem
text == text[::-1]  # False for "Never odd or even"
Fix
normalized == normalized[::-1]

Finished this lesson?

Mark it as learned to track your progress.

Next: List in Python →

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges