Python Palindrome Checker
Check if a string or number is a palindrome (reads the same backward as forward) using Python slicing.
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.
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}")'121' -> Palindrome
'12321' -> Palindrome
'radar' -> Palindrome
'Python' -> Not Palindrome
'A nut for a jar of tuna' -> PalindromeReal-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
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.