write a python program to determine how many occurances of the letter ‘g’ are in the word “strawberry”.
word = "strawberry"
count = 0
for letter in word:
if letter == 'g':
count += 1
print(count)
Explanation:
word = "strawberry": This line assigns the string “strawberry” to the variableword.count = 0: This initializes a variablecountto 0. This variable will store the number of ‘g’s we find.for letter in word:: This loop iterates through each character (letter) in thewordstring. In each iteration, the current character is assigned to the
variableletter.if letter == 'g':: This condition checks if the currentletteris equal to the character ‘g’.count += 1: If the condition is true (the letter is ‘g’), thecountvariable is incremented by 1.print(count): After the loop has finished iterating through the entire word, this line prints the final value ofcount, which represents the total
number of occurrences of the letter ‘g’ in the word.
Output:
0
The word “strawberry” does not contain the letter ‘g’, so the output is 0.