Strings · Python example
Normalize a phrase and compare it with its reverse to detect a palindrome.
A palindrome reads the same forwards and backwards. Real phrases need normalizing: ignore case, spaces, and punctuation first.
text = "Never odd or even"
cleaned = "".join(char.lower() for char in text if char.isalnum())
if cleaned == cleaned[::-1]:
print(f'"{text}" is a palindrome')
else:
print(f'"{text}" is not a palindrome')