Files
30-seconds-of-code/snippets/palindrome.md
Isabelle Viktoria Maciohsek 19f636408c Make expertise a field
2022-03-01 20:27:27 +02:00

524 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Palindrome string intermediate 2018-02-01T10:19:59+02:00 2020-11-02T19:28:27+02:00

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