I am wondering how to count specific letters in a string. The first thing that popped into my head was the function len. Out of curiosity, is there a way to write this code without using built in functions and using len?
There is a question asked similar to this here and I am having trouble understanding it.
def count_letters(word, char):
count = 0
for c in word:
if char == c:
count = count + 1
return count
What exactly is going on in if char == c: and count += 1? I understand why the person started with a for loop but I don't understand why place an if after?
charis the letter you want to count how many times it appears inword. You iterate on the letters ofwordand if it's the letter you want, you add the counter by 1.