Syntax:
The argument of count() method varies based on the data type of the variable in which it will apply. Two types of the syntax of count() method are shown below.
A. string.count(sub-string, start, end)
The above count() method is used for string datatype. Here, the first argument is mandatory and it will contain the value that will be searched in the main string. The other two arguments are optional. When the start optional argument is used only then the method will start the searching from the start position and when both optional arguments are used then it will search the sub-string between the start and the end positions.
B. tuple.count(search-item) or list.count(search-item)
The above count() method is used for tuple or list datatype only and it has only one mandatory argument that will contain the search item. The uses of these two types of count() method are explained in the next part of this article.
Use of count() method in the string without optional argument:
In the following example, the main string and the search string are defined into two variables, main_str, and search_str. The count() method of this script will count how many times the value of search_str appears in the whole value of main_str. Next, the return value of this method will be printed by the formatted string.
# Define the main string where the string will be searched
main_str = 'Eat to live, not live to eat'
# Define the search string that will be searched
search_str = 'live'
# Store the count value
count = main_str.count(search_str)
# Print the formatted output
print("The word "'"%s"'", appears %s times in the following text:\n%s\n" %
(search_str, count, main_str))
Output:
Here, the value of the main text is ‘Eat to live, not live to eat‘, and search text is ‘live’ that appears two times in the main text.
Using count() method in the string with optional arguments:
This example shows the uses of optional arguments of count() method. The start optional argument is used to define the start position of the main text for searching and the end optional argument is used to define the position of the main text where the searching will end. Both the main text and the searching text will be taken as input here. Three types of searching will be done by this script. In the first search, searching text will be searched in the whole content of the main text like the previous example. In the second search, the searching text will start searching from position 20 of the main text. In the third search, the searching text will start searching from position 5 and stop the searching in position 25. So, the following script will generate three outputs of count() method based on the arguments.
# Define the main string where the string will be searched
mainStr = input("Enter the main string\n")
# Define the search string that will be searched
searchStr = input("Enter the search string\n")
# Store the total count value
count = mainStr.count(searchStr)
# Print the formatted output
print("The string "'"%s"'", appears %s times in the main text\n" %
(searchStr, count))
# Store the total count value searching from the position 20
count = mainStr.count(searchStr, 20)
# Print the formatted output
print("The string "'"%s"'", appears %s times in the main text after the position 20\n" %
(searchStr, count))
# Store the total count value searching within the position 5 to 25
count = mainStr.count(searchStr, 5, 25)
# Print the formatted output
print("The string "'"%s"'", appears % s times in the main text within the position 5 to 25\n" %
(searchStr, count))
Output:
Here, ‘Learn Linux from LinuxHint and become a Linux expert’ is taken as the main text, and ‘Linux’ is taken as the searching text. The word ‘Linux’ appears three times in the main text, one time if you start searching from position 20 and two times if you start searching from the position 5 to 25.
Use of count() method in tuple and list:
The following example shows how any particular item can be searched in a tuple and a list. A tuple of the string is defined and printed at the beginning of the script. Next, a search value is taken as input. Search and count how many times the value appeared in the tuple. The script will print the result of the count with the formatted string. A list of the string is also defined and printed, next it searches and counts a particular input value in the list like the tuple.
# Define a tuple
nameTuple = ('John','Janifer','Lisa','John','Lucy','john')
# Display tuple
print(nameTuple)
# Define the search string that will be searched in the tuple
searchStr = input("Enter the search string for tuple\n")
# Store the count value
count = nameTuple.count(searchStr)
# Print the formatted output
print("The string "'"%s"'", appears %s times in the tuple\n" % (searchStr, count))
# Define a list
fruitList = ['Banana','Mango','Orange','Grape','Mango','banana']
# display list
print(fruitList)
# Define the search string that will be searched in the list
searchStr = input("Enter the search string for list\n")
# Store the count value
count = fruitList.count(searchStr)
# Print the formatted output
print("The string "'"%s"'", appears %s times in the list\n" % (searchStr, count))
Output:
The following output will appear if you take ‘John’ as search text for tuple and ‘Mango’ as search text for the list.
Conclusion:
Different uses of count() method in python are tried to show in this article to help the python users to learn how to use this method in their python script.