def isPalindrome(phrase): """take a phrase and convert to all lowercase letters and ignore punctuation marks and whitespaces, if it matches the reverse spelling then it is a palindrome """ phrase_letters = [c for c in phrase.lower() if c.isalpha()] print phrase_letters # test return (phrase_letters == phrase_letters[::-1]) """ phrase1 = "A man, a plan, a canal, Panama!" if isPalindrome(phrase1): print '"%s" is a palindrome' % phrase1 else: print '"%s" is not a palindrome' % phrase1 print """ inputPhrase = raw_input("Enter a Sentence") if isPalindrome(inputPhrase): print '"%s" is a palindrome' % inputPhrase else: print '"%s" is not a palindrome' % inputPhrase phrase2 = "Madam in Eden I'm Adam" if isPalindrome(phrase2): print '"%s" is a palindrome' % phrase2 else: print '"%s" is not a palindrome' % phrase2
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question