In this section, we will learn what String count() method is and how to use it in Python.
What is String count() Method in Python?
The Python string count() method is used to count the number of times a string value is repeated in the target string (the one that invoked this method).
Python String count() Method Syntax:
string.count(value, start, end)
String count() Method Parameter
The method takes three arguments:
- The first argument is the value we want to search for in the target string.
- The second argument, which is optional, is the index position where the search should start from. The default is set to 0.
- The third argument is the index position where the search should stop there. The default is set to the size of the string, meaning the entire string value should be searched for the specified value.
String count() Method Return Value
The return value of this method is a string value that shows the number of times the sub-string (the first argument of the method) is repeated in the target string.
Example: python count characters in string
s1 = "They know we know that they know we know!" result = s1.count("know") print(result)