I need to find how many times a letter appears in a sentence then print the number. The input will look like ‘n How is noon for your party?’ So n needs to come back as two because noon has two ‘n’s in it. My code so far is:
statement = input()
word = statement.split()[0]
letter = str(word)
first_letter = letter[0]
print(statement.find(first_letter)
I’m very new to python I’m sorry.
>Solution :
statement = input()
first_letter = statement[0]
number_of_occurrences = statement.count(first_letter) - 1
print(number_of_occurrences)
Explanation:
The first letter is the first character of the input string (statement). There is no need to split the sentence because when we calculate the number of occurrences of that first letter we just substract by 1 to neglect the first occurrence.