Recursive Function palindrome in Python

Confused about Recursive picture Confused about Recursive · Jun 4, 2009 · Viewed 84.3k times · Source

I need help writing a recursive function which detects whether a string is a palindrome. But i can't use any loops it must be recursive. Can anyone help show me how this is done . Im using Python.

Answer

Unknown picture Unknown · Jun 4, 2009
def ispalindrome(word):
    if len(word) < 2: return True
    if word[0] != word[-1]: return False
    return ispalindrome(word[1:-1])

And here is the best one liner

def ispalindrome(word):
    return word == word[::-1]