Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 826617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:26:35+00:00 2026-05-15T03:26:35+00:00

Python newb here. I m trying to count the number of letter as in

  • 0

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')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:26:36+00:00Added an answer on May 15, 2026 at 3:26 am

    The other answers show what’s wrong with your code. But there’s also a built-in way to do this, if you weren’t just doing this for an exercise:

    >>> 'banana'.count('a')
    3
    

    Danben gave this corrected version:

    def count_letters(word, char):
      count = 0
      for c in word:
        if char == c:
          count += 1
      return count
    

    Here are some other ways to do it, hopefully they will teach you more about Python!

    Similar, but shorter for loop. Exploits the fact that booleans can turn into 1 if true and 0 if false:

    def count_letters(word, char):
      count = 0
      for c in word:
        count += (char == c)
      return count
    

    Short for loops can generally be turned into list/generator comprehensions. This creates a list of integers corresponding to each letter, with 0 if the letter doesn’t match char and 1 if it does, and then sums them:

    def count_letters(word, char):
      return sum(char == c for c in word)
    

    The next one filters out all the characters that don’t match char, and counts how many are left:

    def count_letters(word, char):
      return len([c for c in word if c == char])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python newb here looking for some assistance... For a variable number of dicts in
I'm a python newb and am having trouble groking nested list comprehensions. I'm trying
Alright, I'll preface this with the fact that I'm a GTK and Python newb,
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python uses the reference count method to handle object life time. So an object
Python's convention is that variables are created by first assignment, and trying to read
Python, through it's readline bindings allows for great command-line autocompletion (as described in here
python's time module seems a little haphazard. For example, here is a list of
I am trying to attach a file with a contact form. My code looks
I'm a newb to Python so apologies in advance if my question looks trivial.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.