In this tutorial, we’ll learn what Python string count() method is and how to properly use it with the help of an easy Python code examples.
Introduction: Python String count() Method
This method is used to return the number of occurrences of a substring in a specified string.
Syntax of count() Method
string.count( subString, startRange(optional), endRange(optional) )
- Python string count() method takes three arguments.
- First one is the substring that is to be searched within a string.
- Second parameter is optional. It can be used to specify the starting point(index) from where the search should start.
- Third parameter is also optional. It’s used to specify the ending point(index) where the search should stop.
- This count() method returns the number of specific substring occurrences in a string.
Example 1: count() Method applied on a Python String
stringVal='Python Programming language' print(stringVal.count('P'))
2 // P is used 2 times in the above string
As you can see here that character P is searched using count() method and the result shows that this substring is used 2 times in the specified string.
Example 2: count() Method is Case Sensitive
stringVal='Python programming language' print(stringVal.count('P'))
1 // p is used 2 times but still the output is 1
As we can see in the above string that we’ve two P characters in our string but still the output shows 1. Reason is that we’ve passed a capital P to count method which will check only capital P in the string. Similarly, if we pass small(lowercase) p character then it will only check for lowercase p characters.
In order to solve this, we’ve to use Python lower() method which converts the whole string to lowercase format. See below code:
stringVal='Python programming language' print(stringVal.lower().count('p'))
Just make sure that if you are using the lower() method then pass the substring in lowercase format as well. You can also use upper() method which will first convert the string to uppercase then check the specified substring(use substring in uppercase).
Example 3: Specifying Range in count() Method
stringVal='Python Programming language' print(stringVal.count('P',3))
1 // the search will start from 4th index (0,1,2,3)
In the above code, we’ve specified a starting range from where the search should start. In order to specify both start and end range. See below code:
stringVal='Python Programming language' print(stringVal.count('P',8,14))
The substring comes in the string but not in the specified range. As a result, Python string count method returns 0.
Example 4: count() Method Return Value
stringVal='Python Programming language' returnedValue=stringVal.count('P') print(returnedValue)
Python string count() method returns the number of times the specific substring appeared in a string. If the specified substring is not found then it returns 0.
As a conclusion of this tutorial, hope you now have a detailed, in-depth practical knowledge of how to properly use Python string count method. I’ll be looking forward to have your valuable feedback on this post. Thank you for reading it.