For an exact, case-sensitive check, compare a string with its reverse: text == text[::-1]. That treats every character—including spaces and punctuation—as significant. If you mean a phrase-style palindrome, first decide which characters to keep and whether to ignore case, then compare the normalized text with its reverse.
Check an exact string
Python strings support sequence slicing. The slice [::-1] traverses a string in reverse order, so equality with that reversed string checks whether the original reads identically in both directions, character for character.
As an Amazon Associate I earn from qualifying purchases.
def is_palindrome(text: str) -> bool:
return text == text[::-1]
print(is_palindrome("level")) # True
print(is_palindrome("Python")) # False
This version is case-sensitive and counts spaces and punctuation. For instance, "Level" is not equal to its reverse, "leveL". The approach uses a reversed string for the comparison, making it a concise choice for ordinary short strings. See the Python 3.14 built-in types documentation for string behavior and case folding.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchCheck a phrase while ignoring case and punctuation
Phrase checks need a stated rule. This function keeps letters and digits, uses Unicode-aware case folding, and discards all other characters before comparing:
#1 Best Overall
def is_phrase_palindrome(text: str) -> bool:
normalized = "".join(ch.casefold() for ch in text if ch.isalnum())
return normalized == normalized[::-1]
print(is_phrase_palindrome("A man, a plan, a canal: Panama!")) # True
The phrase normalizes to amanaplanacanalpanama, which is identical to its reverse. The filter deliberately includes digits because isalnum() accepts letters and numbers. If digits should not count, or if you want to preserve punctuation or spaces, change the filtering rule.
What case folding changes
casefold() is intended for caseless matching and can transform some characters differently from lower(). For example, Python documents "straße".casefold() as "strasse". That can change the normalized sequence, so use it when the desired rule is caseless matching rather than merely lowercasing ASCII text. The same documentation describes the str.casefold() method.
Rank #2
Unicode text can contain accents and combining marks in different representations. The function above does not remove accents or normalize those representations; define and implement that policy separately if it matters to your application.
Choose an implementation that fits the task
| Approach | How it works | Best fit | Trade-off |
|---|---|---|---|
| Slicing | Compare the string with text[::-1]. |
A concise check for ordinary strings. | Builds a reversed string. |
| Two pointers | Compare characters at the beginning and end, then move inward until they mismatch or meet. | Showing each comparison step or avoiding a full reversed copy. | Requires more code; apply the same normalization policy first when ignoring characters. |
| Recursion | Compare the outer characters, then repeat the check on the substring between them. | Learning recursive problem solving. | Less direct for everyday checks and subject to recursion-depth concerns. |
There is no need to use recursion just to solve a routine palindrome check. A textbook exercise presents it as a way to practice recursion while considering case-insensitive comparison and non-letter characters at the ends; see John Zelle’s Python Programming: An Introduction to Computer Science, 2nd Edition.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Define the palindrome rule before using the result
- Exact strings: Use the direct slice comparison when case, whitespace, and punctuation must match exactly.
- Phrase-style checks: Choose whether to ignore case, punctuation, spaces, digits, and accents; implement that policy before comparison.
- Learning algorithms: Use two pointers to make the comparisons visible, or recursion when recursive problem solving is the lesson.
For more Python learning materials, Python.org maintains a getting started guide with beginner resources.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




