Files
30-seconds-of-code/snippets/palindrome.md
2019-08-20 11:18:55 +03:00

449 B

title, tags
title tags
palindrome string,intermediate

Returns True if the given string is a palindrome, False otherwise.

Use str.lower() and re.sub() to convert to lowercase and remove non-alphanumeric characters from the given string. Then, compare the new string with its reverse.

from re import sub

def palindrome(string):
  s = sub('[\W_]', '', string.lower())
  return s == s[::-1]
palindrome('taco cat') # True