python palindrome
时间: 2023-10-29 22:57:49 浏览: 106
Palindrome in Python is a word, phrase, number, or other sequence of characters that reads the same backward as forward. Here's an example of a Python function that checks if a given string is a palindrome:
```python
def is_palindrome(word):
# Convert the word to lowercase and remove any non-alphanumeric characters
word = ''.join(e for e in word.lower() if e.isalnum())
# Check if the word is equal to its reverse
return word == word[::-1]
```
You can use this function to check if a word is a palindrome. For example:
```python
print(is_palindrome("racecar")) # Output: True
print(is_palindrome("hello")) # Output: False
```
This function first converts the word to lowercase and removes any non-alphanumeric characters using a list comprehension. Then, it checks if the word is equal to its reverse using slicing. If they are equal, it returns `True`; otherwise, it returns `False`.
阅读全文