This is a tutorial of the Python String count() method. Learn to find the occurrences of a substring in a given string using this method with examples.
The Python String
count() method returns the number of times a particular substring occurs in a given string. You can make use of this method to check how many times a particular character, word, or any other sub-string is appearing in a given string.
Note. This method also allows you to check either the entire given string or only a particular part of the given string to lookup for the occurrences using the start and end arguments. You’ll find more about this in this examples section.
Given below is the syntax for the
OccurrencesCount = givenString.count(subString, start, end)
This method can take upto three different arguments and these are described below.
subString. This is the string that you want to lookup in the given String to count its occurrences. This can be a single character, a word, or any other string. It is mandatory to be provided.
- start. This is an optional argument and it defines the starting index from where you want to start the lookup process in the given string.
- end. This is also an optional argument and it defines the ending index upto where you want to do the lookup process in the given string.
Let’s say if there’s a string of length 10, then it will have the index of the first character as 0 and the index of the last character as 9. Using the start and end argument, you can specify the part of the string to lookup for the occurrences of the particular substring in the given substring. For example,
start = 5 and
end = 8, will do the lookup process between the from the index 5 upto 8.
count() Return Value
It returns an integer value that is the number of times the provided substring is repeated in the given string.
Given below are the two examples demonstrating the usage of the Python String
Example 1. Finding the number of times a substring is occurring in a given String
Here we’re simply looking up how many times the specified substring is actually occurring in the given string. We need not specify the start and end arguments here as we are looking up in the entire string length.
#Given String sentence = "How are you feeling today?" #Counting the occurrences of the letter 'e' count = sentence.count('e') print("e occures", count, "times in the sentence.") #Counting the occurrences of word 'hello' count = sentence.count('hello') print("hello occures", count, "times in the sentence.")
e occures 3 times in the sentence. hello occures 0 times in the sentence.
Example 2. Usage of the Python String
count() Method with
In the following code snippet, we’re specifying the values for the start and end arguments to specify the part of the string which we wanted to be looked up to count the number of occurrences of the substring in the given string.
words = "This sentence contains a few words and this is written just for demonstration!" count = words.count('i', 5, 20) print("Number of times i repeated in words from index 5 to 20:", count) count = words.count('i', 15) print("Number of times i repeated in words after from index 15:", count)
Number of times i repeated in words from index 5 to 20: 1 Number of times i repeated in words after from index 15: 5
In the above example, in the first case, the lookup starts at index 5 in the words string while the lookup ends at index 20 while in the second case the lookup starts from the index 15 and goes till the end of the string.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!