Python String Count() Method

Last Updated : 29 Dec 2025

It returns the number of occurences of substring in the specified range. It takes three parameters, first is a substring, second a start index and third is last index of the range. Start and end both are optional whereas substring is required.

Python String count() Method Syntax

It has the following syntax:

Parameters

  • sub (required)
  • start (optional)
  • end (optional)

Return Type

It returns number of occurrences of substring in the range.

Different Examples for Python String count() Method

Let's see some examples to understand the count() method.

Python String count() Method Example 1

Let us take an example to demonstrate the working of the string count() method in Python.

Output:

occurences: 2

Python String Count() Method Example 2

Let us take an example to demonstrate the working of the string count() method in Python.

Here, we are passing second parameter (start index).

Python String Count() Method Example 3

Let us take another example to demonstrate the working of the string count() method in Python.

Output:

occurences: 5

Python String Count() Method Example 4

The given example is using all three parameters and returning result from the specified range.

Output:

occurences: 1

Python String Count() Method Example 5

It can count non-alphabet chars also, see the given example.

Output:

occurences: 3
 
Next TopicPython Strings