Python OOPs


Python SQLite


Examples


Others


String count() method in Python


The count() method returns the number of occurrences of a substring in the string.

Syntax
string.count(substring,start,end)

Parameters

  • substring - (Required) The substring to count in given string.
  • start - (Optional) The index to start the search. Default is 0.
  • end - (Optional) The index to end the search. Default is the end of the string.

The following example shows, how to use the count() method in python.

Example 1 : Count number of occurrences of a given substring
txt="This is sample text"
n=txt.count("is")
print("The count is ",n)

Output

The count is  2    
Example 2 : Count number of occurrences of a given substring with start and end
txt="This is sample text"
n=txt.count("is",4,10)
print("The count is ",n)

Output

The count is  1    

Python String Methods