The count() function returns the number of substring occurrences in the [start, end] range. Three parameters are required: a substring, the second start index, and the third is the last index in the range. Both start and end are optional, while substring is necessary.
The syntax of the method is: str.count(sub, start = 0,end = len(string))
Key Points :
• The count() function returns the number of occurrences of the substring in the given string.
• This method returns mainly an integer which specifies the number of a substring in a string.
count() string parameters
The count() method needs one execution parameter. However, two optional parameters are available:
• Substring: a string that is to be included in the count.
• start (Optional): The start index is an optional parameter used to specify the search string starting index.
• end (Optional): The end index is an optional parameter used to specify terminating point in the search string.
Below is the python program to demonstrate the count() function:
# Python program to demonstrate the # use of count() function str = "Technology Motivator"; sub = "o"; print (str.count(sub, 12, 20)) sub = "Motivator"; print (str.count(sub))
When we run above the program, the outcome is as follows:
Output : 2 1
Below are several other functions that we can use to work with string in Python 3