Methods and Functions

Python String count()

There may be situations when you wish to know how many times a given value appears in the list or string. Assume you work in human resources. You may be interested in knowing how many employees have brought in new clients or how many employees’ salaries exceed a given threshold.

Counting several repeating objects at the same time is a common programming problem. Python provides a plethora of tools and strategies for approaching this challenge. One such method is the Python count() function which is a String subclass method. It returns the number of occurrences of a specified string from the input string.

Python count() function

Definition

  • The Python count() function is an inbuilt function used to count the number of occurrences of a particular element appearing in the string.
  • Python count() is a string method that returns a numeric value denoting the number of occurrences of a substring in the given input string.

Python count() Syntax

The syntax for the Python string count() method is as follows:

                    

string.count(substring, start, end)

Note – The string count() function is case-sensitive.

count() Parameters

The count() method compulsorily requires 1 parameter. But it also accepts 2 more parameters:

  • substring – the string whose count is to be found
  • start (Optional) – starting index from where the search begins. The default value is 0
  • end (Optional) – ending index of the string where the search ends. The default value is the end of the string

Note 1 – Python string count() accepts only a single substring parameter. If we pass multiple substrings as arguments, it throws a TypeError exception.

Note 2 – Indexing in Python starts from 0, and not 1.

Return value from count()

The python count() method returns an integer value indicating the number of occurrences of the given string.

If the value is not found, the function returns 0.

Example 1: Count the number of occurrences of a given substring

Let’s look at an example of the string count() method.

Example

                    

# Python program to illustrate count()
txt = 'I love Python programming'

# Lowercase o
substring = 'o'
count = txt.count(substring)
print('No. of occurrences of - o: ', count)
# Uppercase O, case-sensitive function
print('No. of occurrences of - O: ', txt.count('O'))

txt = 'I am ill. Will you buy these medicines for me?'
count = txt.count('ill')
print('No. of occurrences of - ill: ', count)

Output

                    

No. of occurrences of - o:  3
No. of occurrences of - O:  0
No. of occurrences of - ill:  2

Example 2: Count the number of occurrences of a given substring using start and end

The start and the end parameters can be used to determine where our search should begin and finish. This gives us more flexibility when utilizing the count() function.

Example

                    

# Python program to illustrate count()
string = 'Good morning, everyone, Welcome back to school'

# search starts from index = 8
c = string.count('o', 8)
print('The count for letter o:', c)

# search starts from index = 10 and ends at index = 25
c = string.count('e', 10, 25)
print('The count for letter e:', c)

Output

                    

The count for letter o: 5
The count for letter e: 3

Example 3: String not found and TypeError exception

If the substring argument is not found in the input string, the count() function returns value 0. Also if the substring argument is passed with more than 1 substring, the Python compiler will throw a TypeError exception.

Example

                    

# Python program to illustrate count() 
my_txt = 'My hobbies are to Dance and Travel'

c = my_txt.count('Read')
print('Count of Read is:', c)
print('')

c = my_txt.count('Dance', 'Travel')
print('Count is:', c)

Output

                    

Count of Read is: 0

Traceback (most recent call last):
File "", line 7, in 
TypeError: slice indices must be integers or None or have an __index__ method

Frequently Asked Questions

Q1. What does count() do in Python?

There may be situations when you wish to know how many times a given value appears in the list or string. Python provides a plethora of tools and strategies for approaching this challenge. One such method is the Python count() function which is a List and a String subclass method.

The Python count() function is an inbuilt function used to count the number of occurrences of a particular element appearing in the string.

The syntax for the Python string count() method is as follows:

                    

string.count(substring, start, end)

Q2. How do you count values in a list in Python?

Python count() is a list method that returns a numeric value denoting the number of occurrences of an element in the given input list.

Example

                    

marks = [2, 5, 8, 2, 5, 3, 8, 9, 5, 2, 5, 6, 7, 5]

c = marks.count(2)
print('Count of 2 is:', c)

c = marks.count(5)
print('Count of 5 is:', c)

Output

                    

Count of 2 is: 3
Count of 5 is: 5

Q3. How do you count characters in Python?

To count specific characters in a string in Python, we use the string count() function. We specify the character as a substring parameter in the count() function. Also, we need to keep In mind that Python string count() accepts only a single substring parameter. If we pass multiple substrings as arguments, it will throw a TypeError exception.

Example

                    

s = 'Swimming is my life-long passion'

c = s.count('i')
print('Count of i:', c)

c = s.count('m')
print('Count of m:', c)

Output

                    

Count of i: 5
Count of m: 3

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.