top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Count character occurrences in a given word, phrase or sentence

Writer's picture: aya abdalsalamaya abdalsalam

we will count how many times an character appears in a word or a phrase or a sentence

There is many ways to count occurrences of a character I will introduce some of them

1. for loop

2. count()

3. regular expression

4. pandas library


For loop

using for loop is a good way to count number of occurrence of a character in a word or sentence

def countWord(word):
    count = 0
    for i in word:
        if i == charToCount:
            count = count + 1
    print("Your character " + charToCount + " is found " + str(count) + " Times")
word = input('Enter your word : ')
charToCount = input('Enter Your character : ')
countWord(word)

output:

Enter your word : Working From home is good
Enter Your character : i
Your character i is found 2 Times

count()

using count function can help you , it will return how many times this character is found in this string or sentence


def countWord(word):
    print("Your character " + charToCount + " is found " + str(word.count(charToCount))+ " Times")
    
word = input('Enter your word:  ')
charToCount = input('Enter Your character : ')
countWord(word)

output:

Enter your word: Working From home is good
Enter Your character: i
Your character i is found 2 Times

Regular Expression

regular expression is a way to search in a string and make matching and by matching you can use many function such as find() , findall() and search() in regular expression you need to import its library which called re

import re
def countWord(word):
    print("Your character " + charToCount + " is found " + str(len(re.findall(charToCount, word))) +" Times")
word = input('Enter your word :  ')
charToCount = input('Enter Your character : ')
countWord(word)

output:

Enter your word:  Working From home is good
Enter Your character: i
Your character i is found 2 Times

Pandas Library

There is a function in pandas called value_counts by it It will count every character count in a new line . You need to pass string as a series object and you need to import pandas

import pandas as pd
def countWord(word):
    print(pd.Series(list(word)).value_counts())
word = input('Enter your word ')
countWord(word)

output:

Enter your word Working From home is good
o    5
     4
r    2
i    2
g    2
m    2
W    1
k    1
n    1
F    1
h    1
e    1
s    1
d    1
dtype: int64

0 comments

Recent Posts

See All

Comments


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