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
		
while True:
	print("Enter 'x' for exit.")
	string = input("Enter any string: ")
	if string == 'x':
		break
	else:
		char = input("Enter a character to count: ")
		val = string.count(char)
		print(val,"\n")

Here is the sample run of the above Python program illustrating how to count total occurrence of particular character in a given string:

python count character in string

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