String count() Method Syntax

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

Parameters: 

  • The count() function has one compulsory and two optional 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 the number of times a substring occurs in a given string. 

Example:

Python3




#initializing a string
my_string = "Banana"
#using string count() method
char_count = my_string.count('a')
#printing the result
print(char_count)


Output

3

Python String count() Method

Python String count() function returns the number of occurrences of a substring within a String.

Python3




#initializing a string
my_string = "Apple"
#using string count() method
char_count = my_string.count('A')
#printing the result
print(char_count)


Output

1

In this article, we will explore the details of the count() method, exploring its syntax, usage, and some practical examples.

Similar Reads

What is the String count() Method?

...

String count() Method Syntax

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

How to use String count() Method

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

More Example on String count() Method

...

Contact Us