You can maintain the count in the parameter itself, like this
def recAlphaCount(s, counter = 0):
if len(s):
counter = recAlphaCount(s[1:], counter + int(s[0].isalpha()))
return counter
This can also be solved like this, without explicitly maintaining the count.
def recAlphaCount(s):
if len(s) == 0:
return 0
elif s[0].isalpha():
return 1 + recAlphaCount(s[1:])
else:
return recAlphaCount(s[1:])
This can be shortened to
def recAlphaCount(s):
if len(s) == 0:
return 0
return int(s[0].isalpha()) + recAlphaCount(s[1:])