Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,25 @@
---
title: Palindrome
type: snippet
language: python
tags: [string]
cover: succulent-6
dateModified: 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.
```py
from re import sub
def palindrome(s):
s = sub('[\W_]', '', s.lower())
return s == s[::-1]
```
```py
palindrome('taco cat') # True
```