Menu

gitpiper

palindrome python Code Snippet in 2024

stringintermediate

Last Updated: 17 December 2024

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

python snippet similar to palindrome For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️