Check a Palindrome in Python
"Never odd or even" reads the same backwards. Clean the text, reverse it with slicing and compare.
- Normalizing text with lower() and filtering
- Reversing with [::-1]
- Comparing the two versions
- 1
"neveroddoreven"Lowercase, drop spaces - 2
cleaned[::-1]Reverse it - 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.
character.lower()🔡 Make every letter the same sizeif character.isalnum()🧹 Remove spaces and punctuationnormalized[::-1]🪞 The reflection, read backwardsnormalized == normalized[::-1]✅ Compare with the mirror imageA palindrome is text that reads the same forward and backward once differences such as capital letters, spaces and punctuation are ignored.
Key points
character.lower()turns every character into lowercase, so"N"and"n"count as the same letter.if character.isalnum()keeps only letters and digits and drops spaces."".join(...)glues the kept characters back into one string,normalized.normalized[::-1]is the same string backwards; comparing it withnormalizedgivesTrue.
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
text = "Never odd or even"
normalized = "".join(character.lower() for character in text if character.isalnum())
print(normalized == normalized[::-1])
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Comparing before cleaning
Capital letters and spaces make real palindromes fail.
text == text[::-1] # False for "Never odd or even"
normalized == normalized[::-1]
Finished this lesson?
Mark it as learned to track your progress.
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.
- Palindrome String Check whether text reads the same forwards and backwards. Easy
- Recursive Palindrome Check a palindrome recursively, ignoring case and punctuation. Medium
- Palindrome Number Check whether a number reads the same backwards. Easy