Write a Python program to count occurrences of a character in a string.

How to count occurrences of a character in a string in Python. Write a Python program to count occurrences of a character in a string.

How to count occurrences of a character in a string?

Given a string containing alphanumeric characters, ascertain the sum of all numbers present in the string.

What is the count of occurrences of a character in a string?

Input: Hello welcome to the computer world
Please enter the Character you want to count: o
Output: The total Number of Times o has Occurred = 5

Algorithm to count occurrences of a character in a string:

• Input a string and the character you want to search.
• Initialize a count variable to 0.
• Iterate using a for loop the whole and check using if statement whether the current character and the searching character is equal or not.
• On getting the matching character increment the count variable.
• Print count variable.

Python program to count occurrences of a character in a string.

string = input("Please enter the String : ")
char = input("Please enter the Character you want to count: ")

count = 0
for i in range(len(string)):
    if(string[i] == char):
        count = count + 1

print("The total Number of Times ", char, " has Occurred = " , count)

Output: Printing the count occurrences of a character in a string.