A palindrome reads the same forwards and backwards. 121 and 1221 are palindrome numbers, and “Madam” and “Level” are palindrome words. In this lesson you will write Python programs to check a palindrome number and a palindrome string, including sentences with spaces and punctuation.
Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.
Palindrome number: reverse the digits
The idea is to reverse the number digit by digit and compare the result with the original.
def is_palindrome_number(n):
original = n
reverse = 0
while n > 0:
digit = n % 10
reverse = reverse * 10 + digit
n = n // 10
return original == reverse
for number in [121, 123, 1221, 7]:
print(number, is_palindrome_number(number))Output:
121 True 123 False 1221 True 7 True
How it works
n % 10 gives the last digit, and n // 10 removes the last digit. Here is the loop for n = 121:
Step | digit | reverse | n after the step |
|---|---|---|---|
1 | 1 | 1 | 12 |
2 | 2 | 12 | 1 |
3 | 1 | 121 | 0 |
The loop stops when n becomes 0. The reverse is 121, which equals the original, so 121 is a palindrome.
This function treats 0 as a palindrome and returns False for negative numbers because the loop does not run for them.
A shorter way: compare as strings
Convert the number to a string and compare it with its reverse. The slice [::-1] reverses a string.
n = 1221
print(str(n) == str(n)[::-1])Output:
True
Palindrome string
For text, we should ignore capital letters, spaces and punctuation. The function below keeps only letters and digits, changes them to lowercase, and then compares the text with its reverse.
def is_palindrome_text(text):
cleaned = ''.join(ch.lower() for ch in text if ch.isalnum())
return cleaned == cleaned[::-1]
for text in ['Madam', 'python', 'A man, a plan, a canal: Panama', 'Was it a car or a cat I saw?']:
print(text, '->', is_palindrome_text(text))Output:
Madam -> True
python -> False
A man, a plan, a canal: Panama -> True
Was it a car or a cat I saw? -> Truech.isalnum() is True for letters and digits, so spaces and punctuation are dropped. ”.join() puts the kept characters back together into one string.
Take the input from the user (on your computer)
text = input('Enter a word or sentence: ')
if is_palindrome_text(text):
print('Palindrome')
else:
print('Not a palindrome')Our online compiler does not support input() yet, so use a fixed value there.
Common mistakes
- Not changing the case. “Madam” is a palindrome, but its reverse, “madaM”, looks different unless you convert both to lowercase.
- Not removing spaces and punctuation in sentence palindromes.
- Modifying n inside the loop and then comparing with n. Save the original number first.
Practice questions
- Check whether 12321 is a palindrome without converting it to a string.
- Find all palindrome numbers between 100 and 200.
- Check whether a list such as [1, 2, 3, 2, 1] is a palindrome.
- Write a program that counts the palindrome words in a sentence.
FAQs
Is a single-digit number a palindrome?
Yes. It reads the same in both directions.
How do I reverse a string in Python?
Use slicing: text[::-1].
Is 0 a palindrome?
Yes, in the number program above, 0 gives True.
