Counts occurrences of a substring
count() method returns the number of times the substring sub appears in the string.
You can limit the search by specifying optional arguments start and end.
|sub||Required||Any string you want to search for|
|start||Optional||An index specifying where to start the search.|
Default is 0.
|end||Optional||An index specifying where to stop the search.|
Default is the end of the string.
# Count occurrences of 'Big' in the string S = 'Big, Bigger, Biggest' x = S.count('Big') print(x) # Prints 3
Limit count() Search to Substring
If you want to search the string from the middle, specify the start parameter.
# Count occurrences of 'Big' from 5th character S = 'Big, Bigger, Biggest' x = S.count('Big',5) print(x) # Prints 2
You can specify where to stop the
count() search with end parameter.
# Count occurrences of 'Big' between 5th to 13th character S = 'Big, Bigger, Biggest' x = S.count('Big',5,13) print(x) # Prints 1
Optional arguments start and end are interpreted as in slice notation.
S.count('Big',5,13) is similar to