45.2k views
5 votes
Write a Python function to check if a given string is a palindrome. A palindrome is a word, phrase, or sequence of characters that reads the same forwards and backwards. Ignore spaces, punctuation, and capitalization when checking for palindromes.​

User Balexand
by
9.3k points

1 Answer

2 votes

Answer:

def is_palindrome(s):

# Remove spaces and punctuation, and convert to lowercase

s = ''.join(e for e in s if e.isalnum()).lower()

# Compare the original string with its reverse

return s == s[::-1]

# Test cases

print(is_palindrome("A man, a plan, a canal, Panama")) # True

print(is_palindrome("race car")) # True

print(is_palindrome("hello")) # False

User Allan Cameron
by
8.3k points