String method count() in Python
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.
Syntax
 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
String method count()
 1 msg = "Hi Python, Python is programming language !"
 2 
 3 # counting string
 4 s1 = msg.count("Python")
 5 s2 = msg.count("Python", 8)
 6 
 7 # print occurrence
 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.
Output
 1 2  Occurrence found !
 2 1  Occurrence found !

Example 2

Example 2
 1 str1 = "string in string !"
 2 
 3 s1 = str1.count('string')
 4 
 5 print(s1, 'occurrence present in source string.')
Output
 1 2 occurrence present in source string.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us