Python - String Method - count() Tutorial
It will return the number of occurrences of a substring in the given string. It is case sensitive
Syntax-
String.count(value, start, end)
#String- Value from where the substring needs to be searched
#value - It is a substring that needs to be searched in the given String.
#start - (Optional) It is a position to start the search, by default is 0
#end - (Optional) It is a position to end search,
by default is the end of the string
Example-
str1 = "Hello Python, Hello Fresherbell, Hello"
print(str1.count("Hello"))
print(str1.count('Hello',6,26))
print(str1.count("hello"))
Output-
3
1
0
In the above program,
Line 2- "Hello" is searched in whole string str1, and return count 3
Line 3- "Hello" is searched in string str1 index from 6 to 26, and return count 1
Line 4- "hello" is searched in whole string str1, and return count 0. because the count function will perform a case-sensitive search in whole string.