To count the occurrence of character or to find frequency of character in a string in python, you have to ask from user to enter a string and then ask to enter a character to count total occurrence of that character in the given string and finally print the result on the output screen as shown in the program given below.
Following python program ask from user to enter string and character to count occurrence of character in string or to find frequency of character in string:
# Python Program - Count Character in String print("Enter 'x' for exit."); string = input("Enter any string to count character: "); if string == 'x': exit(); else: char = input("Enter a character to count to count from above string: "); val = string.count(char); print("Total = ",val);
Here is the sample run of the above Python program illustrating how to count total occurrence of particular character in a given string:
Above one is the screenshot of initial output, now you have to enter any string say this is codescracker and then to enter any character say i and then press enter key to check what is the total number of occurrence of the character i present inside the entered string which is this is codescracker here:
Same program on python shell:
You may also like to learn or practice the same program in other popular programming languages: