codescracker


python

Python Program to Count Character in String



« Previous Program Next Program »


Count Character in String in Python

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.

Python Programming Code to Count Character in String

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:

count character in string python

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:

count character program python

Same program on python shell:

python count character in string

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test