""" palindrome.py A recursive function to recognize a palindrome. Jim M | GPL | Nov 2011 """ def clean_string(string): """Return string as lowercase, keeping only letters. >>> clean_string("The cat.") 'thecat' """ result = "" for char in string.lower(): if char in 'abcdefghijklmnopqrstuvwxyz': result = result + char return result def palindrome(string): """Retrun true if string is a palindrome. >>> palindrome('cat') False >>> palindrome('mom') True >>> palindrome('') True """ if len(string) < 2: return True if string[0] != string[-1]: return False else: return palindrome(string[1:-1]) def main(): while True: phrase = raw_input('What is the phrase? (nothing to quit) ') if not phrase: print "... bye" return phrase = clean_string(phrase) print "Your string cleaned up is '%s'." % phrase if palindrome(phrase): print "Yup, it's a palindrome." else: print "Nope, it isn't a palindrome." if __name__ == "__main__": import doctest doctest.testmod() main()