How does count () work in python?

The count() is a built-in function in Python. It will return the total count of a given element in a string. The counting begins from the start of the string till the end. It is also possible to specify the start and end index from where you want the search to begin.

In this Python tutorial, you will learn:

  • Python count
  • The syntax for PythonString Count()
  • Example 1: Count Method on a String
  • Example 2: Count occurrence of a character in a given string
  • Example 3: Count occurrence of substring in a given string

The syntax for PythonString Count()

Python count function syntax:

string.count(char or substring, start, end)

Parameters of Python Syntax

  • Char or substring: You can specify a single character or substring you are wants to search in the given string. It will return you the count of the character or substring in the given string.
  • start : (optional) It indicates the start index from where the search will begin. If not given, it will start from 0. For example, you want to search for a character from the middle of the string. You can give the start value to your count function.
  • end: (optional) It indicates the end index where the search ends. If not given, it will search till the end of the list or string given. For example, you don’t want to scan the entire string and limit the search till a specific point you can give the value to end in your count function, and the count will take care of searching till that point.

ReturnValue

The count() method will return an integer value, i.e., the count of the given element from the given string. It returns a 0 if the value is not found in the given string.

Example 1: Count Method on a String

The following example shows the working of count() function on a string.

str1 = "Hello World"
str_count1 = str1.count('o')  # counting the character “o” in the givenstring
print("The count of 'o' is", str_count1)

str_count2 = str1.count('o', 0,5)
print("The count of 'o' usingstart/end is", str_count2)

Output:

The count of 'o' is 2
The count of 'o' usingstart/end is 1

Example 2: Count occurrence of a character in a given string

The following example shows the occurrence of a character in a given string as well as in by using the start/end index.

str1 = "Welcome to Guru99 Tutorials!"
str_count1 = str1.count('u')  # counting the character “u” in the given string
print("The count of 'u' is", str_count1)

str_count2 = str1.count('u', 6,15)
print("The count of 'u' usingstart/end is", str_count2)

Output:

The count of 'u' is 3
The count of 'u' usingstart/end is 2

Example 3: Count occurrence of substring in a given string

Following example shows the occurrence of substring in a givenstring as well as usingstart/endindex.

str1 = "Welcome to Guru99 - Free Training Tutorials and Videos for IT Courses"
str_count1 = str1.count('to') # counting the substring “to” in the givenstring
print("The count of 'to' is", str_count1)
str_count2 = str1.count('to', 6,15)
print("The count of 'to' usingstart/end is", str_count2)

Output:

The count of 'to' is 2
The count of 'to' usingstart/end is 1

Summary:

  • The count() is a built-in function in Python. It will return you the count of a given element in a list or a string.
  • In the case of a string, the counting begins from the start of the string till the end. It is also possible to specify the start and end index from where you want the search to begin.
  • The count() method returns an integer value.

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.

How does count () work in python?

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

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)

How does count () work in python?

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

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.

Conclusion

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.

How do you use the count command in Python?

The count() method returns the number of occurrences of a substring in the given string..
substring - string whose count is to be found..
start (Optional) - starting index within the string where search starts..
end (Optional) - ending index within the string where search ends..

How do you write a count in Python?

Syntax of count() Function in Python Python Count() function has following syntax: string. count(substring/character, start=, end=)

How do you count a list in Python?

There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.

How do you count inputs in Python?

Python String has got an in-built function – string. count() method to count the occurrence of a character or a substring in the particular input string. The string. count() method accepts a character or a substring as an argument and returns the number of times the input substring happens to appear in the string.