msh210
0
Q:

python count character occurrences

# initializing string  
test_str = "Grepper Answer"
  
# using count() to get count  
# counting e  
counter = test_str.count('e') 
>> 3
9
# using count() to get count  
# counting e  
counter = test_str.count('e') 
1
# Python3 code to demonstrate  
# each occurrence frequency using  
# naive method  
  
# initializing string  
test_str = "GeeksforGeeks"
  
# using naive method to get count  
# of each element in string  
all_freq = {} 
  
for i in test_str: 
    if i in all_freq: 
        all_freq[i] += 1
    else: 
        all_freq[i] = 1
  
# printing result  
print ("Count of all characters in GeeksforGeeks is :\n "
                                        +  str(all_freq)) 
0
str1.count("a")
0

New to Communities?

Join the community