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 do i count a letter in a word in python?

I need to count how many excact letter in a word.
E.g i need to count the letter "i" in these word.
And i need to find which word has the most "i" letter in it.


list = ['box', 'computer', 'sheep', 'family', 'illegal', 'bubble', 'lamp', 'circuit', 'building', 'plant', 'these', 'cup']
def fgv(list):
    counter= 0
    maximal = 0
    word= "x"
    for e in list:
        counter = 0
        if e.__contains__("i"):
            for i in range (len(e)):
                if e[i] == "i":
                    couner += 1
        if counter < maximal:
            maximal = counter
            word = e
    return(word)                   
print(f"{fgv(list)}")

>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

def count_letter(list):
    counter = 0
    maximal = 0
    word = ""
    for e in list:
        counter = e.count("i")
        if counter > maximal:
            maximal = counter
            word = e
    return word

word_list = ['box', 'computer', 'sheep', 'family', 'illegal', 'bubble', 'lamp', 'circuit', 'building', 'plant', 'these', 'cup']
print(count_letter(word_list))

try this code

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