Letter Count on a string

Posted by user74283 on Stack Overflow See other posts from Stack Overflow or by user74283
Published on 2010-05-28T21:04:07Z Indexed on 2010/05/28 21:12 UTC
Read the original article Hit count: 190

Filed under:

Python newb here. I m trying to count the number of letter "a"s in a given string. Code is below. It keeps returning 1 instead 3 in string "banana". Any input appreciated.

def count_letters(word, char):
    count = 0
    while count <= len(word):
        for char in word:
            if char == word[count]:
                count += 1
            return count



print count_letters('banana','a')

© Stack Overflow or respective owner

Related posts about python