top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

Character occurrence

Think of the len() function, but this method has a much scope enabling you to use of bigger strings or character occurrences. We use dictionaries iterated over by for loops under a function.

In the dictionary, we take count of characters over a loop

# Count character occurrences in a given word, phrase or sentence

def charCount(s):

# create an empty dictionary


for i in s:

if i not in charLib:



return charLib

#test function

occurrences= charCount("ASHIOYA LOVES DATA SCIENCE")

occurrences.pop(' ') # to delete space character if it is in dictionary

for char,occur in occurrences.items():

print(char+" contains "+str(occur)+" times ")

N/B The pop() is ONLY used if it's in a string otherwise it wouold be an error

1 comment

Recent Posts

See All

1 Comment

Data Insight
Data Insight
Sep 20, 2021

Use code snippet functionality to insert your code.

bottom of page