Python string length recursion

John Redyns picture John Redyns · Apr 14, 2011 · Viewed 13.2k times · Source

I'm blanking out trying to write a simple function to count the string length recursively.

I can do sums, fibonacci, and factorial easy but I'm trying to create the simplest function with only one parameter, I don't like having a second just as a counter index..

Can any post something small for me?

Answer

Donovan picture Donovan · Apr 14, 2011

Is this what you are looking for?

def recursiveLength(theString):
    if theString == '': return 0
    return 1 + recursiveLength(theString[1:])