Find count of character in string python

What you will learn here about python

  • Find count of character in string python

Find count of character in string is one of the common python interview question. Here we will see how to find count of character in string using python.

Find count of character in string python

#! /usr/bin/python3

def findCount(string):
    hashMap = {}

    for i in range(0,len(string)):
        if string[i] in list(hashMap.keys()):
            hashMap[string[i]] += 1
        else:
            hashMap[string[i]] = 1

    return hashMap


result = findCount("INFOSYS")
print(result)


OUTPUT

{‘I’: 1, ‘N’: 1, ‘F’: 1, ‘O’: 1, ‘S’: 2, ‘Y’: 1}

You may also like...