Python Count – List count() and String count() Method

Python has a built-in function called count(). It will return the total number of elements in a list for a specified element. The count() function counts elements in both a list and a string.

Python List count()

Python has a built-in function called count(). It will provide you with the count of a specific list element.

Syntax of count list python:

list.count(element)

Parameters:

  • element: The element for which you want to calculate the count.
  • ReturnValue: The count() method returns an integer value, indicating the number of elements in the supplied list. If the value isn’t found in the given list, it returns 0.

The count() method on a list is demonstrate in the following example:

Example 01:

colors = ['red', 'green', 'blue', 'orange','blue']
color_count = colors.count('blue')
print('The count of color: blue is ', color_count)

Output:

img1

Example 02:

numbers = [2,3,5,9,10,6,9,9]
num_count = numbers.count(9)
print('The count of element: 9 is ', num_count)

Output:

img2

In the case of a list, the count() function must be given the element to be counted, and it will return the count of the element. The count() method returns an integer value.

Python String count()

Python String count() function is an inbuilt function in a python programming language that returns the number of occurrences of a substring in the given string.

Syntax of count String python:

string.count(substring, start=…, end=…)

Parameters:

  • Mandatory parameter:
    • substring: string whose count is to be found.
  • Optional Parameters:
    • start (Optional): starting index within the string where the search starts.
    • end (Optional): ending index within the string where the search ends.
  • Return Value: count() method returns an integer that denotes number of times a substring occurs in a given string.

The count() method on a String is demonstrate in the following example:

Example 01: Implementation of the count() method without optional parameters

# Python program to demonstrate the use of
# count() method without optional parameters 
  
# string in which occurrence will be checked
string = "Hello Welcome to Softhunt.net We Welcome you" 
  
# counts the number of times substring occurs in 
# the given string and returns an integer
print(string.count("Welcome"))

Output:

2

Example 02: Implementation of the count() method using optional parameters

# Python program to demonstrate the use of
# count() method without optional parameters 
  
# string in which occurrence will be checked
string = "Hello Welcome to Softhunt.net We Welcome you" 
  
# counts the number of times substring occurs in 
# the given string between index 0 and 5 and returns 
# an integer
print(string.count("Welcome", 0, 5))
  
print(string.count("Welcome", 0, 15))

Output:

0
1

Conclusion

Count() is a built-in function. It will provide you with the count of a particular element in a list or string.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. floor() and ceil() function in python

Leave a Comment