Palindrome program in Python

Check if a string or a number is a palindrome in Python with slicing, two pointers and a loop, and ignore spaces and case. Run every example in your browser.

Short answer

A string is a palindrome when s == s[::-1]. For a number, compare str(n) with its reverse. To ignore spaces, punctuation and capital letters, keep only ch.isalnum() characters, lowercased, before you compare.

Method 1: compare the string with its reverse

s[::-1] is a slice that walks the string backwards, so it builds the reversed string. If the two are equal, the word reads the same both ways.

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

print(is_palindrome("racecar"))
print(is_palindrome("hello"))
Output
True
False

Method 2: two pointers (no copy of the string)

Compare the first and last characters, then move both ends inward. The first mismatch settles it. This uses no extra memory and is the answer interviewers usually expect after the slicing one-liner.

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("abba"))
print(is_palindrome("abca"))
Output
True
False

Method 3: palindrome number without converting to a string

Build the reversed number digit by digit: n % 10 gives the last digit and n // 10 drops it. A negative number is never a palindrome, because of the minus sign.

def is_palindrome_number(n):
    if n < 0:
        return False
    original, reverse = n, 0
    while n > 0:
        reverse = reverse * 10 + n % 10
        n //= 10
    return original == reverse

print(is_palindrome_number(12321))
print(is_palindrome_number(123))
Output
True
False

Method 4: ignore spaces, punctuation and case

Sentences like “A man, a plan, a canal: Panama” are palindromes only once you drop everything except letters and digits and lowercase them.

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

print(is_palindrome_sentence("A man, a plan, a canal: Panama"))
print(is_palindrome_sentence("Madam"))
Output
True
True

Common mistakes

  • Forgetting case: "Madam" == "madaM" is False. Lowercase first if capital letters should not matter.
  • Using reversed(s) and comparing it with s. reversed returns an iterator, not a string, so the comparison is always False. Use "".join(reversed(s)) or s[::-1].
  • Moving the pointers with while left <= right and then stepping past each other. left < right is enough; the middle character of an odd-length word always matches itself.

Practice: your turn

Write solve(s) that returns True if s reads the same forwards and backwards, otherwise False. Try the two-pointer version: compare the outermost pair, then move inward.

This challenge and its tests come from the PyMaster course. Press Run tests to check your function.

main.py
Input for input()

Runs real Python 3.14 in your browser with Pyodide. Your code is not uploaded. Ctrl + Enter runs the tests. Press Esc, then Tab, to leave the editor.

Hint 1

A palindrome mirrors around its middle.

Hint 2

Compare the outermost pair, then move both pointers inward; any mismatch settles it.

Hint 3

Compare s[left] and s[right]; a mismatch means False. If the pointers meet, return True.

Show a solution
def solve(s):
    left, right = 0, len(s) - 1
    while left < right:
        if s[left] != s[right]:
            return False
        left += 1
        right -= 1
    return True

Questions

Is an empty string a palindrome?

Yes. It reads the same both ways, and "" == ""[::-1] is True. A single character is a palindrome too.

How do I check a palindrome number entered by the user?

Read it with n = input(), which is already a string, then compare n == n[::-1]. Convert with int() only if you need the number for something else.

Which method is fastest?

Slicing is the fastest in Python because it runs in C, even though it makes a copy. Two pointers use less memory and can stop at the first mismatch.

What are some palindrome words to test with?

racecar, level, madam, noon, refer, and numbers such as 121, 1331 and 12321.

More Python programs

All Python programs Online Python compiler