Strings · beginner
Python Program to Check if a String is a Palindrome
Check whether a string or number is a palindrome in Python with slicing ([::-1]), ignoring case, spaces and punctuation, plus a two-pointer version.
- slicing [::-1]
- str.lower()
- str.isalnum()
- generator expressions
A palindrome reads the same forwards and backwards: "level", "racecar", or the number 12321. For sentences such as "A man, a plan, a canal: Panama" people usually ignore case, spaces and punctuation, so the program cleans the text before comparing.
The check is a common interview warm-up because it has an easy answer (compare the text with its reverse) and a follow-up that asks you to do it without making a reversed copy. Both are shown below.
The program
def is_palindrome(text):
cleaned = "".join(ch.lower() for ch in text if ch.isalnum())
return cleaned == cleaned[::-1]
for text in ["racecar", "Python", "A man, a plan, a canal: Panama", "12321"]:
print(repr(text), is_palindrome(text))'racecar' True
'Python' False
'A man, a plan, a canal: Panama' True
'12321' TrueHow it works
- ch.isalnum() keeps only letters and digits, dropping spaces, commas and colons.
- ch.lower() makes the comparison case-insensitive, so "A" matches "a".
- "".join(...) glues the kept characters back into one string: "amanaplanacanalpanama".
- cleaned[::-1] is the string reversed (a slice with step -1). If it equals the original, the text is a palindrome.
Watch it run line by line
Visualize the call for the Panama sentence and look at cleaned in the is_palindrome frame: all spaces and punctuation are gone before the comparison.
“Visualize step by step” opens the program in the Python Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.
Another way: Two pointers, without building a reversed copy
Compare the first and last characters, then move inwards. This uses no extra memory for a reversed string and stops at the first mismatch, which is how the problem is solved in interviews.
def is_palindrome(s):
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
print(is_palindrome("level"), is_palindrome("levels"))
print(is_palindrome(str(12321)))True False
TrueCommon mistakes
- Comparing without lower(): "Racecar" is then not a palindrome because "R" != "r".
- Calling reversed(text) and comparing it with text: reversed() returns an iterator, not a string, so the comparison is always False. Use "".join(reversed(text)) or text[::-1].
- Passing an integer: 12321[::-1] raises TypeError. Convert with str() first.
Try it yourself
- Find the palindromic numbers between 100 and 200.
- Find the longest palindromic word in a sentence.
Open the program in the Python Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.
Related examples
Browse all 25 Python examples, or write your own program in the online Python Compiler.