The string method count() used to find the occurrence of specified value in the given string. It returns a number that specifies that how many times a specified string occur in source string from the given boundary (start and end index) value.
1 <string>.count(<search>, <start>, <end>)
string : A source string to search for the given occurrence
search : A search string to find the occurrence in the source string, It is case sensitive and Required
start : It specifies the start index for the count function, if not specified, the default will be 0, Optional
end : It specifies the end index to end the search, if not specified, string length will be considered, Optional
1 msg = "Hi Python, Python is programming language !"
2
3
4 s1 = msg.count("Python")
5 s2 = msg.count("Python", 8)
6
7
8 print(s1, " Occurrence found !")
9 print(s2, " Occurrence found !")
In the above example, a string variable is define with initial value. A count() method called by using string that start searching specified string from index 0 in first example, and in second example, it starts from index 8. The result will be printed on console.
1 2 Occurrence found !
2 1 Occurrence found !
Example 2
1 str1 = "string in string !"
2
3 s1 = str1.count('string')
4
5 print(s1, 'occurrence present in source string.')
1 2 occurrence present in source string.
Related options for your search