Count function in Python string

In this Python tutorial, I will show you what is count function in Python string. we will also see what will be its syntax, parameters, and return values. and examples for the count() function in Python string.

Python, a general-purpose programming language, offers a wide array of built-in methods that can be utilized for various purposes. One such method is Python count() function.

What is the Count() function in Python string?

The count() function in Python string is a built-in method that helps to determine the frequency of a specified element within a given object. For strings, it counts the number of occurrences of a substring i.e., the number of times a specific substring appears in a string.

Syntax:

The syntax of the count() method for strings in Python is: Here, str is the original Python string.

str.count(sub, start= 0,end=len(string))

Parameters:

The count() function only requires a single parameter for execution. However, it also has two optional parameters:

  • sub: It is the substring that we want to count the number of occurrence in the original Python string.
  • start(optional): Starting index number within the Python string where the search starts.
  • end(optional): Ending index number within the Python string where the search ends.

Return value:

The count() method in Python string returns the number of occurrences of the substring in the given string. If the substring isn’t found, the method returns 0.

Python count() function with examples

The count function in Python string has a variety of applications. Here are some practical examples that demonstrate its usage:

Example-1: To count the number of occurrences of a substring

This is the basic use of the count method in string Python

For instance, we have stored the names of the cities of different people from an event held in the US in the form of a Python string with commas. And we have to count the number of peoples came from ‘Texas’ in the event:

states = "California, Texas, New York, Texas, California, California, Texas"

count = states.count("Texas")
print('Number of people came from Texas is:', count)

The output is: Here, states.count(“Texas”) is the function call in Python. We pass “Texas” as the argument to the count() function. The function will search through the states Python string, count the number of times “Texas” appears, and return this count. The count is then stored in the count variable, which we print to see the result.

Number of people came from Texas is: 3
Python count function in String

This way we can use string.count() to find the number of occurrence of a substring in a Python string.

Example-2: Use the Python count method to find a substring within a specific range

Here, we will use the start and end parameters to search a specific substring within the range in a Python string.

For example, suppose we have a Python string that lists all the companies where a person has gone to perform events from 2000 to 2008, let’s check how many times he visited ‘microsoft’ between 2000 to 2008:

states = "Apple, Facebook, Microsoft, Apple, Amazon, Microsoft, Walmart, Apple, Microsoft"

count = states.count("Microsoft", 0, len(states))

print('Number of times visited Microsoft is: ', count)

The output is:

Number of times visited Microsoft is:  3
Count function in Python string counting within a range

This way we count the number of substring occurrence within a range using the count function in Python string.

Example-3: Checking if the substring is present or not in Python string

As we know, if the substring is not present in the Python string then the Python count() function will return 0.

For example, Suppose we have a Python string that lists the items of the cart elements which we wanted to buy in the sale, let’s check do we have laptop in the string or not:

items = "Watch, Iphone, Camera, Headset, PS-4"

count = items.count('Laptop')

print(count)

The output is:

0
Count() method Python string check 0 or 1

Hence, this shows that the item(Laptop) is not present in the Python string. And this way we can use the count() string Python to check if the substring is present or not.

Conclusion

In this, we have seen what is count function in Python string. We have also seen the syntax, parameters, and return value of count() function and many examples.

Hence, we can say that the count() function is an incredibly useful tool in Python, enabling programmers to determine the number of times a specific substring occurs within a string. It can be applied in various scenarios, such as text analysis and data processing.

You may also like to read: