top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Character Count using Python

Updated: Sep 9, 2021

The python code illustrated below allows a user to count the number of occurrences of alphabets in a word or sentence. The code uses loop statement to count the alphabets.


First an input is asked from the user which is converted into a list. The repetition of letters is then removed by converting the list into a set (Note that the set data type can only contain unique elements which helps to remove the duplication of the letters).


Then every occurrences of a letter in set is checked in the list and every time the letter is encountered, the counter is increased by one. This is achieved by the for loop. The final counter (c) value for a letter is then appended into a new list and the output is displayed. Here I have made use of string formatting to display the output in the end.


The overall program looks as:

user_input = input('Enter a word or sentence: ')
letters = list(user_input.upper())
sorted_letters = list(sorted(set(letters)))
count = []
for s in sorted_letters:
    c = 0
    for x in letters:
        if s == x:
            c += 1
    count.append(c)
for i in range(len(sorted_letters)):
    if (i+1)<len(sorted_letters):
        print('There are {} letter {} in the provided word/sentence'.format(count[i+1],sorted_letters[i+1]))

If we breakdown the above code, we see the following:

  • user_input asks the user for input (word or sentence)

  • letters stores the alphabets of user_input

  • sorted_letters removes the duplicate character from letters (notice the sorted() function, this allows to store the characters in ascending order)

  • count is an empty list to store the value of occurrences of characters

  • the for loop (first one) is used to count the occurrence of the character

  • the for loop (second one) is used to print the number of occurrences (notice in this for loop how the printing is done from i+1 index and not i - this is to remove the space between words in the sentence as space occurs first when arranging the characters in ascending order)

A sample of the program run is shown below:

Here the characters are converted to upper case since python recognizes lower and upper case as difference characters by their ASCII value.

The entire code with their steps are provided in Git repository.


2 Comments


Aina Adekunle
Aina Adekunle
Sep 10, 2021

Very well explained and implemented.

Like

Aina Adekunle
Aina Adekunle
Sep 09, 2021

Nice! But you didn't give any example using the program.

Like

COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page