Palindrome check in Python
A string-cleaning warm-up built on slicing.
def is_palindrome(text):
cleaned = "".join(ch.lower() for ch in text if ch.isalnum())
return cleaned == cleaned[::-1]
How it works
- Keeps only letters and digits, lowercased.
[::-1]reverses the cleaned text for comparison.- Equal means it reads the same both ways.
Keywords and builtins used here
defforifis_palindromereturn
The run, in numbers
- Lines
- 3
- Characters to type
- 117
- Tokens
- 39
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 26 seconds.
Step 6 of 9 in Strings, step 12 of 72 in Language basics.