About 97,400 results
Open links in new tab
  1. python - Letter Count on a string - Stack Overflow

    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,...

  2. python - Count the number of occurrences of a character in a …

    Jul 20, 2009 · This should be downvoted because it is the least efficient way possible to count characters in a string. If the goal is simply to count characters, as the question indicates, it would be hard to find a worse way to do the job. In terms of memory and processor overhead, this solution is definitely to be avoided. No one will ever "need" to use ...

  3. How to count characters in a string? (python) - Stack Overflow

    Mar 28, 2014 · As for counting characters in a string, just use the str.count() method: >>> s = "Green tree" >>> s.count("e") 4 If you are just interested in understanding why your current code doesn't work, you are printing 1 four times because you will find four occurrences of 'e', and when an occurrence is found you are printing len(scr) which is always 1.

  4. How to count digits, letters, spaces for a string in Python?

    sample = ("Python 3.2 is very easy") #sample string letters = 0 # initiating the count of letters to 0 numeric = 0 # initiating the count of numbers to 0 for i in sample: if i.isdigit(): numeric +=1 elif i.isalpha(): letters +=1 else: pass letters numeric

  5. list - counting letters in a string python - Stack Overflow

    May 30, 2014 · If the word contains more than one of a certain letter, for example "bees", when it iterates over this, it will now count the number of 'e's twice as the for loop does not discriminate against unique values. Look at string iterators, this might clarify this more. I'm not sure this will solve your problem, but this is the first thing that I noticed.

  6. how to count characters in a string in python? - Stack Overflow

    Apr 1, 2020 · This is what it returns: The number of characters in this string is: 1 The number of characters in this string is: 2 The number of characters in this string is: 3 The number of characters in this string is: 4 The number of characters in this string is: 5. Is there a way to only print the last line so that it prints:

  7. Counting letters in a string with python - Stack Overflow

    Jun 26, 2013 · Count the number of occurences of letters in a Python string. 22. ... Count number of letters in string ...

  8. python - For Loop to Count Letters in a Word - Stack Overflow

    def count_letters(a): count=0 for char in a: if char == 'a': count+=1 return count In general, it is considered more pythonic to avoid for loops. While not the shortest solution (see comment by Eric Duminil) to your problem, this might give an idea how to use list comprehension instead:

  9. pythonic way to count total number of letters in string in python

    Sep 18, 2020 · I just started learning python, and I can think of two ways to count letters in a string (ignoring numbers, punctuation, and white spaces) Using for loop: for c in s: if c.isalpha():

  10. Count multiple letters in string Python - Stack Overflow

    Sep 5, 2015 · I'm trying to count the letter's 'l' and 'o' in the string below. It seems to work if i count one letter, but as soon as i count the next letter 'o' the string does not add to the total count. What am I missing? s = "hello world" print s.count('l' and 'o') Output: 5