Site icon Machine Learning Projects

Python Program to Count the Number of Occurrences of a Character in a String – 2023

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Count the Number of Occurrences of a Character in a String.

Example 1: Using a for loop

count = 0

my_string = "Machine Learning Projects"
my_char = "e"

for i in my_string:
    if i == my_char:
        count += 1

print(count)

Output

3

Example 2: Using method count()

my_string = "Machine Learning Projects"
my_char = "e"

print(my_string.count(my_char))

Output

3

Check out our other python programming examples

Exit mobile version