count() method is used to count items in a list or string. The
count() method can count how many times an item appears in a list and how many times a substring appears in a string.
Often, when you’re working with a list or a string, you’ll want to know how many times a specific value appears in the list or the string. For example, you may be a wholesaler who wants to know how many orders a specific customer has placed over the last six months.
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. We’ll also run through a couple of examples to show the
count() method being used in a Python program.
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 so you can assess demand.
In order to find out this information, you could use a
count() method on a list of tea orders placed over the last month by a certain customer. 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)
Our code returns:
3. Let’s break down the example program we have written. On the first line, we declare an array that stores all the orders a particular customer has placed in the last month. Then, we use the
count() method and specify the argument
English Breakfast to find out how many times that tea has been ordered by the customer.
Finally, we print out the result of our
count() method to the console, which returns 3 in this case.
In addition, 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)
The count method returns:
Python String Count
In addition, the
count() method can be used to count the number of occurrences of a substring within a larger string. The Python string method
count() searches through a string and returns a value equal to the number of times a
substring appears in the string.
The syntax for the string
count() method is as follows:
string_name.count(substring, start_pos, end_pos)
count() method takes three parameters:
substringis the string whose count is to be calculated with the larger string (required)
start_posis the index position at which the search should begin (optional)
end_posis the index position at which the search should stop (optional)
Let’s use an example to showcase the string
count() method. Say that we have a 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, with the only difference being that we have specified a string rather than a list.
In addition, we can use the
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 in 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.
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 code 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. So, even though Emily has been on the monthly honor roll twice, she has only appeared on the list once before the index position 49 in our list.
count() method in Python can be used to calculate how many times a particular value appears within a string or a list in Python. When the method is used with a string, it will search for a
substring within a larger string and return the number of times that
substring appears in the string. In addition, when the method is used with a list, it will return the number of times a particular value has been entered into the list.
In this tutorial, we discussed how to use the
count() method on both strings and lists to determine the number of times a value appears within a string or a list. We also went through a few examples of these methods in action.
You now have the knowledge you need to use the Python count method like a pro!
Are you interested in learning about the job market for Python developers? Download the free Career Karma app today to learn more about salaries and jobs for Python developers from our expert coaches.