python_reference/useful_scripts/palindrome.py

15 lines
440 B
Python
Raw Normal View History

2014-04-13 19:52:40 +00:00
# Sebastian Raschka 04/2014
def palindrome(my_str):
"""
2014-04-13 19:54:44 +00:00
Returns True if an input string is a palindrome. Else returns False.
2014-04-13 19:52:40 +00:00
"""
2014-04-13 20:26:15 +00:00
stripped_str = "".join(l.lower() for l in my_str if l.isalpha())
2014-04-13 19:52:40 +00:00
return stripped_str == stripped_str[::-1]
if __name__ == '__main__':
test1 = 'Hello World!'
test2 = "Go hang a salami. I'm a lasagna hog."
print('test1', palindrome(test1))
print('test2', palindrome(test2))