Skip to content

Latest commit

 

History

History
23 lines (18 loc) · 491 Bytes

palindrome.md

File metadata and controls

23 lines (18 loc) · 491 Bytes
title type language tags cover dateModified
Palindrome
snippet
python
string
succulent-6
2020-11-02

Checks if the given string is a palindrome.

  • 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, using slice notation.
from re import sub

def palindrome(s):
  s = sub('[\W_]', '', s.lower())
  return s == s[::-1]

palindrome('taco cat') # True