Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need to find how many times a letter appears. (python3)

enter image description hereI 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading