count() method in Python String can be used to count the number of occurrences of a given substring in an original string.
The count() method returns an integer value denoting how many times a particular substring is found in an original string.
Syntax
mainstring.count(substring)
This search can be limited if we specify beginning as well as ending point as the parameters in the above syntax.
mainstring.count(substring, beg, end)
s1 = "Python loves me. I love Python."
s2 = "Python"
res = s1.count(s2)
print(res)
O/P
2
Let’s try to understand what happened in the above code :
We tried to count the occurrence of “Python” in the original string. Hence, we got 2 as our output.
One thing to note, Strings are case sensitive, hence, “Python” is not same as “python”.
That’s all here.
