Python Program to Check Palindrome (String and Number)

Short answer
A palindrome reads the same forwards and backwards. In Python, clean the text with lower() and compare it to its reverse: s == s[::-1].

Palindrome checks are a classic first string exercise because they combine slicing, string methods and a simple comparison. Words such as level and radar are palindromes, and so are phrases such as "A man, a plan, a canal: Panama" once you ignore spaces, punctuation and capital letters.

The core idea is tiny, but real inputs are messy. This page shows the shortest solution, a version that handles sentences, a two-pointer version that avoids building a reversed copy, and an interactive program that asks the user for text.

Quick facts
InputA string (or an integer converted to a string)
OutputTrue or False (or a printed message)
ApproachNormalise the text, then compare it with its reverse
Time complexityO(n) time, O(n) extra space for the slice version

Approach to check a palindrome step by step

  1. Normalise the text: make it lowercase and keep only letters and digits, so "Madam, I'm Adam" and "madamimadam" are treated alike.
  2. Reverse the cleaned text. The slice text[::-1] walks the string from the end to the start with a step of -1.
  3. Compare the cleaned text with the reversed copy using ==.
  4. Return the boolean so the caller decides what to print.

When the input is a number, convert it with str() first, because integers cannot be sliced. See converting an integer to a string for details.

How the palindrome code works

The helper is_palindrome() builds cleaned with a generator expression: ch.lower() for every character where ch.isalnum() is true. Spaces and punctuation fail that test and are dropped. "".join(...) glues the survivors back into one string.

The last line, cleaned == cleaned[::-1], already evaluates to True or False, so there is no need for an if/else that returns the same values. An empty string is treated as a palindrome, which is the usual convention.

Python
def is_palindrome(text):
    cleaned = "".join(ch.lower() for ch in text if ch.isalnum())
    return cleaned == cleaned[::-1]

print(is_palindrome("Level"))
print(is_palindrome("Python"))

Other ways to test for a palindrome in Python

reversed(): list(text) == list(reversed(text)) works but is slower and longer than slicing for plain strings. Two pointers: compare the first and last character, then move inward. It stops at the first mismatch and never creates a reversed copy, which matters for very long inputs. Numbers: str(n) == str(n)[::-1] is the easy route; the arithmetic route (peeling off digits with % and //) is a nice exercise but not faster in Python.

MethodExtra memoryStops early on mismatch?Best for
Slice s[::-1]Yes, one reversed copyNoShort, readable code
Two pointers with whileNoYesLong strings, interviews
reversed() with joinYesNoSeeing how iterators work

Examples you can run

Press Run, then edit the code and run it again.

Check a word and a sentence

The helper ignores case, spaces and punctuation, so full sentences work too.

Check a word and a sentence
def is_palindrome(text):
    cleaned = "".join(ch.lower() for ch in text if ch.isalnum())
    return cleaned == cleaned[::-1]

print(is_palindrome("Racecar"))
print(is_palindrome("A man, a plan, a canal: Panama"))
print(is_palindrome("Hello"))
Expected outputTrue
True
False

Two-pointer version without a reversed copy

left moves right and right moves left until they meet. The first mismatch ends the loop early.

Two-pointer version without a reversed copy
def is_palindrome(text):
    cleaned = [ch.lower() for ch in text if ch.isalnum()]
    left, right = 0, len(cleaned) - 1
    while left < right:
        if cleaned[left] != cleaned[right]:
            return False
        left += 1
        right -= 1
    return True

print(is_palindrome("noon"))
print(is_palindrome("abca"))
Expected outputTrue
False

Edge cases: empty text, one character and numbers

An empty string and a single character read the same both ways. Convert numbers with str() before slicing.

Edge cases: empty text, one character and numbers
def is_palindrome(text):
    text = str(text)
    return text == text[::-1]

print(is_palindrome(""))
print(is_palindrome("x"))
print(is_palindrome(12321))
print(is_palindrome(1234))
Expected outputTrue
True
True
False

Interactive palindrome checker

Read a word with input() and print a friendly message.

Interactive palindrome checker
word = input("Enter a word: ")
cleaned = word.strip().lower()
if cleaned == cleaned[::-1]:
    print(f"{word} is a palindrome")
else:
    print(f"{word} is not a palindrome")
Expected outputEnter a word: Madam
Madam is a palindrome

Common mistakes

Ignoring case and punctuation

✗ Wrong

def is_palindrome(text):
    return text == text[::-1]

print(is_palindrome("Madam"))
False (because "M" is not equal to "m")

✓ Right

def is_palindrome(text):
    cleaned = "".join(ch.lower() for ch in text if ch.isalnum())
    return cleaned == cleaned[::-1]

print(is_palindrome("Madam"))

Lowercase the text and drop non-alphanumeric characters before comparing.

Slicing an integer

✗ Wrong

n = 121
print(n == n[::-1])
TypeError: 'int' object is not subscriptable

✓ Right

n = 121
print(str(n) == str(n)[::-1])

Integers have no indexes. Convert to a string with str() first.

Python Program to Check Palindrome (String and Number): questions people ask

How do I check if a string is a palindrome in Python?

Compare the string with its reverse: s == s[::-1]. For sentences, lowercase it and remove spaces and punctuation first so only letters and digits are compared.

Is an empty string a palindrome?

By the usual convention, yes. It has nothing that differs between the forward and backward reading, and "" == ""[::-1] is True. Decide explicitly if your program should reject empty input.

How do I check if a number is a palindrome in Python?

Convert it with str() and compare with the reversed string: str(n) == str(n)[::-1]. Negative numbers are normally not palindromes because of the minus sign.

Keep learning

Step-by-step lessons:
Python SlicingPython StringsPython String Methods

Related:
How to Reverse a String in PythonPython Anagram Program: Check if Two Strings Are AnagramsPython Program to Count Vowels in a StringPython String isalnum() Method

More in string programs:
Python Program to Count Vowels in a StringPython Anagram Program: Check if Two Strings Are AnagramsPython Word Count Program: Count Words in a StringPython Program to Count Character Frequency in a String See all →