๐งช Programming
โฑ๏ธ 12s
๐ 1191 chars
๐ฆ 494.03M
๐ง Q4_K_M
Prompt: Write a Python function is_palindrome(s) that returns True if s is a palindrome. One test case.
Response
Here's the code for `is_palindrome()` in Python:
```python
def is_palindrome(s):
"""
Checks whether the input string 's' is a palindrome.
Args:
s (str): The input string to check
Returns:
bool: True if 's' is a palindrome, False otherwise.
"""
# Remove all non-alphabetic characters and convert to lowercase
cleaned_s = ''.join(c.lower() for c in s if c.isalpha())
# Check if the cleaned version of the string matches its reverse
return cleaned_s == cleaned_s[::-1]
```
To use this function, simply call it with a string as an argument:
```python
>>> is_palindrome("A man, a plan, a canal, Panama")
True
>>> is_palindrome("Hello, World!")
False
```
Explanation:
- The `isalpha()` method converts all non-alphabetic characters to
lowercase using the `str.lower()` method.
- Using slicing (`[::-1]`), we reverse the cleaned string and compare it wi
with its original version. If they match, then the input string is a
palindrome.
This solution handles cases where the input string contains non-alphabetic
characters by removing them before checking for the palindrome property.