Last modified: Nov 01, 2026

Python Palindrome Check: Program with Examples

A palindrome reads the same forward and backward. Words like "radar" and "level" are palindromes. Numbers like 121 also count. Checking for palindromes is a classic Python exercise. It teaches string manipulation and loops.

This guide shows several ways to check a palindrome in Python. Each method has strengths and weaknesses. You will see clear examples with output. You will also learn how to handle spaces, punctuation, and case.

What Is a Palindrome?

A palindrome is a sequence that reads the same in both directions. For strings, this means the first character equals the last. The second equals the second last. And so on.

Simple examples include "madam", "racecar", and "noon". A single character is always a palindrome. An empty string is also considered a palindrome by convention.

Real-world checks often ignore spaces and punctuation. For example, "A man, a plan, a canal: Panama" is a palindrome when cleaned. Case also matters. "Radar" and "radar" should match. So normalization is a key step.

Method 1: Check with Slicing

Slicing is the simplest Python approach. Use the slice [::-1] to reverse the string. Then compare the original with the reversed version. If they are equal, it is a palindrome.

 text = "radar" is_pal = text == text[::-1] # Reverse with slicing and compare print(is_pal) # Display the boolean result 
 True 

This method is short and fast. It creates a reversed copy of the string. For small strings, memory use is fine. For very large strings, the copy may be costly.

Slicing works for any sequence, not just strings. You can use it on lists and tuples. That makes it a general-purpose tool.

Method 2: Check with reversed() and join()

The built-in reversed function returns an iterator. You can join the characters back into a string. Then compare with the original.

 text = "level" rev = "".join(reversed(text)) # Build reversed string print(text == rev) 
 True 

This approach is readable but slightly slower than slicing. It creates an iterator and a new string. For most cases, the difference is tiny. Choose based on clarity.

If you need to join characters after filtering, see this guide on joining a list of strings in Python. It covers separators and conversion of non-string values.

Method 3: Two-Pointer Loop

The two-pointer method compares characters from both ends. Start with one pointer at the beginning. Start the other at the end. Move them inward until they meet.

 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("racecar")) # Check with two pointers 
 True 

This method avoids creating a reversed copy. It uses constant extra memory. That makes it efficient for long strings. It also stops early when a mismatch is found.

The two-pointer approach is a good choice for interviews. It shows you understand loops and indexing. It also handles large inputs well.

Handling Case and Spaces

Real input often has mixed case and spaces. Normalize the string first. Convert to lowercase and remove spaces. Then check the cleaned string.

 text = "A man a plan a canal Panama" clean = text.replace(" ", "").lower() # Remove spaces and lowercase print(clean == clean[::-1]) 
 True 

To handle case properly, you can use Python's upper and lower case methods. This guide on Python upper and lower case strings explains lower, casefold, and more. For Unicode text, casefold is safer than lower.

If you need to split the text into words first, see this guide on splitting a string into a list. You can split, filter, and then rejoin. That gives you full control over cleaning.

Handling Punctuation

Punctuation also breaks palindrome checks. The string "racecar!" is not a palindrome if the exclamation mark remains. Remove non-letter characters before comparing.

 import re text = "A man, a plan, a canal: Panama!" clean = re.sub(r"[^a-z]", "", text.lower()) # Keep letters only print(clean == clean[::-1]) 
 True 

The sub function replaces non-letters with an empty string. This keeps only a-z after lowercasing. For Unicode letters, adjust the pattern. Use \w or explicit ranges if needed.

Another option is a list comprehension with str.isalpha(). It keeps only alphabetic characters. Then join the result. This avoids regular expressions for simple cases.

 text = "Hello, World!" clean = "".join(ch for ch in text.lower() if ch.isalpha()) print(clean) 
 helloworld 

Edge Cases

Test empty strings and single characters. Both should return True. They are palindromes by definition. Your function should handle them without errors.

Also test strings with only spaces or punctuation. After cleaning, they become empty. The result should be True. Decide if that behavior fits your use case.

Numbers can also be palindromes. Convert them to strings first. Then apply the same logic. For example, 12321 becomes "12321".

 num = 12321 s = str(num) print(s == s[::-1]) # Check numeric palindrome 
 True 

Performance and Best Practices

Slicing is the fastest for short strings. It is concise and easy to read. Use it when memory is not a concern.

The two-pointer loop uses less memory. It is better for very long strings. It also stops early on mismatches. That can save time on non-palindromes.

Normalize input once. Avoid repeated lowercasing or replacing. Store the cleaned string in a variable. Then reuse it for the check.

Keep functions small and focused. A single function that cleans and checks is fine. But separating cleaning from checking can improve testability. Write small unit tests for edge cases.

When printing results, format them clearly. Compare Python string formatting methods to choose the best style. Clear output helps with debugging and logs.

Common Mistakes

Forgetting to normalize case is a common bug. "Radar" and "radar" should match. Always lowercase or casefold before comparing.

Ignoring punctuation leads to false negatives. Clean the string before the check. Use regex or a comprehension to keep only letters.

Assuming count of characters matters is another mistake. Palindromes are about order, not frequency. Do not confuse the two. For frequency checks, see this guide on counting occurrences in a string.

Also watch for off-by-one errors in loops. The two-pointer method must stop when left >= right. Test with even and odd lengths. Both should work correctly.

Conclusion

Checking for a palindrome in Python is easy. Use slicing for the shortest code. Use a two-pointer loop for memory efficiency. Normalize case and remove punctuation for real-world input. Test edge cases like empty strings and single characters. With these methods, you can write clean and reliable palindrome checks.