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

How I can count letter in sentence in Python

Please help in writing a program that will count the letters ‘a’ in a sentence. I need to use a ‘for’ loop.

text = input("Give a sentence and I will count the number of letters 'a'\n") 
for letter in text:
    a_text = text.count('a')
    print(a_text)

>Solution :

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

text = input("Give a sentence and I will count the number of letters 'a'\n")

count = 0
for letter in text:
    if letter == 'a':
        count += 1

print("The number of 'a' is:", count)
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