1str_x = "He is a good programmer. He Is good. He is he he he he he "
2
3count1 = str_x.count("He") # Counts the word "He" in the string. Remember, case sensitive!
4count2 = str_x.count("he") #Counts the word "he" in the string. Remember, case sensitive!
5
6print(count1 + count2) # Shows the total count of the word "He" in console
1def count_substring(string, sub_string):
2 c = 0
3 while sub_string in string:
4 c += 1
5 string = string[string.find(sub_string)+1:]
6 return c
7
1def count_substring(string,sub_string):
2 l=len(sub_string)
3 count=0
4 for i in range(len(string)-len(sub_string)+1):
5 if(string[i:i+len(sub_string)] == sub_string ):
6 count+=1
7 return count
8