Python Palindrome Checker

Check if a string or number is a palindrome (reads the same backward as forward) using Python slicing.

Try Python Palindrome Checker Code

How it Works

A palindrome is a sequence of characters or digits that reads identically in both directions, ignoring spaces, punctuation, and capitalization.

Python strings support slicing, which allows us to create a reversed copy using `[::-1]`. This makes palindrome checking extremely concise.

For numbers, we can convert the value to a string first or use math loops to reconstruct the digits in reverse order.

Source Code

Verifying multiple alphanumeric string and numeric values for palindrome symmetry.

palindrome.py
Try in Editor
def is_palindrome(val):
    s = str(val).lower().replace(" ", "")
    return s == s[::-1]

test_inputs = [121, 12321, "radar", "Python", "A nut for a jar of tuna"]
for item in test_inputs:
    status = "Palindrome" if is_palindrome(item) else "Not Palindrome"
    print(f"'{item}' -> {status}")
Terminal Output
'121' -> Palindrome
'12321' -> Palindrome
'radar' -> Palindrome
'Python' -> Not Palindrome
'A nut for a jar of tuna' -> Palindrome

Real-world Applications

  • Word puzzle and text games development
  • DNA sequence pattern matching algorithms
  • Technical interview screening challenges

Frequently Asked Questions

How can you check palindromes without converting numbers to strings?

You can extract digits from right to left using % and //, reconstruct the reversed number, and check if it matches the original.

More Examples