Programs & Examples
Strings · Study material
Sentence palindrome checker
Keep only letters and digits, lower-case them, and compare the result with its reverse.
def is_palindrome(text):
letters = "".join(ch.lower() for ch in text if ch.isalnum()) # keep letters/digits only
return letters == letters[::-1]
for t in ["Madam", "A man, a plan, a canal: Panama", "LearnFrenzy", "No 'x' in Nixon"]:
print(f"{t!r}: {is_palindrome(t)}")
'Madam': True 'A man, a plan, a canal: Panama': True 'LearnFrenzy': False "No 'x' in Nixon": True
Formatting a report line
The formatting example on the Introduction tab shows alignment, decimals, thousands separators, zero padding and the f"{name=}" debugging form.