The Python count() method calculates how many times a particular element appears in a list or a string. count() accepts one argument: the value for which you want to search in the list. The count() method is appended to the end of a list or a string object.
You may want to know how many times a specific value appears in the list or the string. For example, say you are a wholesaler. You may want to know how many orders a specific customer has placed over the last six months.
The Python count() method can be used to count the number of times a particular item appears in a list or a string. When used with a string, the count() method counts the number of times a substring appears in a larger string. When count() is used with a list, the method counts the number of occurrences of a specified value in a list.
In this article, we’re going to discuss how to use the count() method on both lists and strings.
Python count() Function
The Python count() function works out how many times a value appears in a list or a string. It returns the number of times the value appears as an integer.
Here is the syntax for the count() function:
names = ["Lewis", "Jade", "Lewis"] print(names.count("Lewis"))
Our code counts how many times “Lewis” appears in our list.
You can see that the count() method is added after a list value in this example. count() does not accept a list as an argument. The count() method expects that you pass the value for which you want to search in a list or a string as an argument.
Python: Count Items in List
For example, let’s say that you are a salesperson for a local tea company. You may want to know how many times English Breakfast tea has been sold over the last month to a particular customer. This will let you assess demand of the tea.
You could use a count() method on a list of tea orders placed over the last month by a certain customer to assess demand. Here’s an example of program that accomplishes this task:
tea_orders_for_customer = ['English Breakfast', 'English Breakfast', 'Oolong', 'Fruit', 'Oolong', 'English Breakfast'] count_orders = tea_orders_for_customer.count('English Breakfast') print(count_orders)
On the first line, of code we declare a Python array that stores all the orders a particular customer has placed in the last month. We use the count() method to calculate how many times the English Breakfast tea has been ordered by the customer.
Finally, we print out the result of our count() method to the console. We do this using a Python print() statement. Our code returns the total number of times the English Breakfast element in the list appears. The result is 3 in this case.
The count() function in Python can be used on a list of numbers. Say you are a fourth grade teacher who wants to know how many ten-year-olds are in your class. You could calculate this information using the following code:
student_ages = [9, 9, 9, 8, 9, 10, 10, 10, 9, 10] count_ten_year_olds = student_ages.count(10) print(count_ten_year_olds)
We count the element four times in our list. The program returns: 4.
count() Python: Using Strings
The count() method can count the number of occurrences of a substring within a larger string. The Python string method count() searches through a string. It returns a value equal to the number of times a substring appears in the string.
The syntax for the count() built-in function is as follows:
string_name.count(substring, start_pos, end_pos)
The string count() method takes three parameters:
- substring is the string whose count is to be calculated with the larger string (required)
- start_pos is the index position at which the search should begin (optional)
- end_pos is the index position at which the search should stop (optional)
count() Python Example: Using Strings
Let’s use an example to showcase the string count() method. Say that we have a Python string that contains the month-by-month honor roll for our class. We want to know how many times Emily appears on the honor roll. We could use the following code to calculate this number:
honor_roll = 'Frank Peter Emily Carly Sophie Alice Miles Frank Emily' count_emily = honor_roll.count('Emily') print(count_emily)
Our code returns: 2.
Our code works similar to the list count() method. The difference is that in this example we have specified a string rather than a list.
Using the Start Position Argument
We can use the start_pos and end_pos arguments to specify where our search should start. Say that we want to know how many times Frank has appeared on the honor roll since he first appeared.
To calculate this data, we want to ignore the first time he appeared on the honor roll. We could do so using the following code:
honor_roll = 'Frank Peter Emily Carly Sophie Alice Miles Frank Emily' count_frank = honor_roll.count('Frank', 6) print(count_frank)
Our code returns: 1.
While Frank appears on our honor roll list twice, the first time he appears is before the index position 6 in our string. Because we have specified the start_pos argument and set it to 6, our program ignores all characters before that index value. So, our code returns 1, rather than 2.
Using the End Position Argument
Or let’s say that we want to find out how many times Emily was on the honor roll before her most recent win. We could calculate this using the following code:
honor_roll = 'Frank Peter Emily Carly Sophie Alice Miles Frank Emily' count_emily = honor_roll.count('Emily', 0, 49) print(count_emily)
Our count() method returns the number of times Emily was on the honor roll. The Python program returns: 1.
We have specified both a start_pos and an end_pos argument in our example. The end_pos argument is set to 49, which is before the second Emily appears in our string.
Emily has been on the monthly honor roll twice. But, her name has only appeared on the list once before the index position 49 in our list.
The count() method in Python calculates how many times a particular value appears within a string or a list in Python. count() accepts one argument: the value for which you want to search in the string or list.
When count() is used with a string, it will search for a substring within a larger string. Then, count() will return the number of times that substring appears in the string. When count() is used with a list, it will return the number of times a particular value has been entered into the list.
You now have the knowledge you need to use the Python count method like a pro! To find more Python learning resources, check out our complete How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more