Simple palindrome function for single words I have created a simple palindrome function that works only on single words. It is working as intended, but I feel that it definitely needs improvement.

def is_palindrome(w):

    if not isinstance(w, str):
        return "ERROR: input is not a string."

    elif len(w) &gt; 2:
        if w[0] == w[-1]:
            while len(w) &gt; 2:
                w = w[1:-1]
                is_palindrome(w)

        if len(w) == 1:
            return True

        else:
            return False

    else:
        return "ERROR: input contains two or less characters."


print is_palindrome('aibohphobia')
print is_palindrome('definitely')


Can you please review my function and post your suggestions?

Also, is it better to do w[:] = w[1:-1] instead of w = w[1:-1]?
Firstly, your testing is insufficient. This code thinks that "defad" is a palindrome and that "deed" is not. You algorithm is completely broken.

def is_palindrome(w):

    if not isinstance(w, str):
        return "ERROR: input is not a string."


Don't check types. Just treat it like a string, and trust the caller to pass the right kind of object. Also don't return strings as errors, throw exceptions.

    elif len(w) &gt; 2:
        if w[0] == w[-1]:
            while len(w) &gt; 2:
                w = w[1:-1]
                is_palindrome(w)


You call the function but you don't do anything with the result. 

        if len(w) == 1:
            return True

        else:
            return False


This is equivalent to: return len(w) == 1    

    else:
        return "ERROR: input contains two or less characters."


Why can't palindromes be zero or one characters? Also, throw exceptions, don't return strings.