Search code examples
pythoncountletter

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

  • 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