Python Program - Count the Number of Occurrence of a Character in String

https:‮gi.www//‬iftidea.com

Here's a Python program that counts the number of occurrences of a character in a string:

def count_char(string, char):
    count = 0
    for c in string:
        if c == char:
            count += 1
    return count

string = input("Enter a string: ")
char = input("Enter a character: ")
print("The character", char, "appears", count_char(string, char), "times in the string.")

Here's an example output:

Enter a string: hello world
Enter a character: o
The character o appears 2 times in the string.