Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

occurences of character in string python

python program to get a String and a character then count the occurrence of character in the string.

Sample Input 1 :

Hello o

Sample Output 1 :

1

Program or Solution

					    
							
s=input("Enter a String:")
c=input("Enter a Character:")
count=0
for i in range(0,len(s)):
    if(s[i]==c):
        count+=1
print(count)

					    
					

Program Explanation

Visit each location in string s one by one for i in range(0,len(s)): if character c is found, increase the count by 1