Skip to content
Related Articles

Related Articles

Improve Article
Python String | count()
  • Difficulty Level : Basic
  • Last Updated : 30 Sep, 2019

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

Syntax:

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

Parameters:

The count() function has one compulsory and two optional parameters.
Mandatory parameter:
1)substring – string whose count is to be found.



Optional Parameters:
1)start (Optional) – starting index within the string where search starts.
2)end (Optional) – ending index within the string where search ends.

Return Value:
count() method returns an integer that denotes number of times a substring occurs in a given string.

Below is the Python 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 = "geeks for geeks" 
  
# counts the number of times substring occurs in 
# the given string and returns an integer
print(string.count("geeks"))

Output:

2

Below is the Python implementation of the count() method using optional parameters.




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

Output:

1
2

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :