2020-05-22 09:57:23 +00:00
|
|
|
def is_palindrome(s: str) -> bool:
|
2020-04-19 16:35:36 +00:00
|
|
|
"""
|
2022-10-23 16:12:49 +00:00
|
|
|
Determine if the string s is a palindrome.
|
|
|
|
|
|
|
|
>>> is_palindrome("A man, A plan, A canal -- Panama!")
|
2020-04-19 16:35:36 +00:00
|
|
|
True
|
|
|
|
>>> is_palindrome("Hello")
|
|
|
|
False
|
2020-05-22 09:57:23 +00:00
|
|
|
>>> is_palindrome("Able was I ere I saw Elba")
|
|
|
|
True
|
|
|
|
>>> is_palindrome("racecar")
|
|
|
|
True
|
|
|
|
>>> is_palindrome("Mr. Owl ate my metal worm?")
|
|
|
|
True
|
2020-04-19 16:35:36 +00:00
|
|
|
"""
|
2022-10-23 16:12:49 +00:00
|
|
|
# Since punctuation, capitalization, and spaces are often ignored while checking
|
|
|
|
# palindromes, we first remove them from our string.
|
|
|
|
s = "".join(character for character in s.lower() if character.isalnum())
|
2020-04-19 16:35:36 +00:00
|
|
|
return s == s[::-1]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-10-23 16:12:49 +00:00
|
|
|
s = input("Please enter a string to see if it is a palindrome: ")
|
2020-04-19 16:35:36 +00:00
|
|
|
if is_palindrome(s):
|
2022-10-23 16:12:49 +00:00
|
|
|
print(f"'{s}' is a palindrome.")
|
2020-04-19 16:35:36 +00:00
|
|
|
else:
|
2022-10-23 16:12:49 +00:00
|
|
|
print(f"'{s}' is not a palindrome.")
|