Python Reference Library →

Python String count() Method – Count Substring Occurrences

Let’s look at string count() method – count substring occurrences

  1. Notice

    count() counts non-overlapping occurrences of a substring. Optional start and end bounds restrict the search.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'aaaa'
result = text.count("aa")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'aaaa'Object 2 · int2
This step

count() counts non-overlapping occurrences of a substring. Optional start and end bounds restrict the search.

Picture string count() method – count substring occurrences

count() counts non-overlapping occurrences of a substring. Optional start and end bounds restrict the search.

Image brief

Alt text
A diagram for Python String count() Method – Count Substring Occurrences. Instructions: text = 'aaaa' result = text.count("aa") print(result). Displayed result: 2. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'aaaa'; object 2: 2
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String count() Method – Count Substring Occurrences". Teaching focus: count() counts non-overlapping occurrences of a substring. Optional start and end bounds restrict the search. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'aaaa' result = text.count("aa") print(result) | v DISPLAYED RESULT 2 FINAL BINDINGS text --> object 1 (str) result --> object 2 (int) object 1: 'aaaa' object 2: 2 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String count() Method – Count Substring Occurrences. Instructions: text = 'aaaa' result = text.count("aa") print(result). Displayed result: 2. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'aaaa'; object 2: 2

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'aaaa'
result = text.count("aa")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'aaaa'Object 2 · int2
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
2

What do you expect this program to print?

What does this example display?

text = 'aaaa'
result = text.count("aa")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

2

Follow the printed values in order. The output is: 2

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

count() counts non-overlapping occurrences of a substring. Optional start and end bounds restrict the search.

count() counts non-overlapping occurrences of a substring. Optional start and end bounds restrict the search.

More examples and reference

Python String count() Method

The count() method in Python is used to count how many times a substring appears in a given string. It is one of the most useful string methods for text analysis and data cleaning.

Syntax

string.count(substring, start, end)

Parameters:

  • substring – The string you want to search for (required)
  • start – The position to start searching (optional)
  • end – The position to stop searching (optional)

Returns:

  • An integer value representing the number of times the substring appears within the given range.

Example 1: Basic Usage

text = "apple banana apple orange apple"
print(text.count("apple"))
3

Example 2: With Start and End Parameters

text = "apple banana apple orange apple"
print(text.count("apple", 10, 30))
1

Use Cases

  • Finding how often a word appears in a sentence
  • Basic data analysis on text fields
  • Validating inputs or detecting duplicates

Case Sensitivity

text = "Python python PyThOn"
print(text.count("python"))
1

Note: count() is case-sensitive. To count in a case-insensitive way, convert the string using lower() or upper() first.

Common Mistakes

  • Forgetting case sensitivity: "Apple" and "apple" are counted differently.
  • Incorrect range: Ensure you use proper start and end index positions.

Interview Tip

count() is often used in problems like finding duplicates, analyzing text patterns, or simple statistics questions.

Summary

  • count() returns how many times a substring appears in the string.
  • You can specify optional start and end positions.
  • It is case-sensitive by default.

Practice Problem

Write a program that reads a sentence from the user and prints how many times the word "the" appears (case-insensitive).

sentence = input("Enter a sentence: ")
word_count = sentence.lower().count("the")
print("Count of 'the':", word_count)

What to remember

Connect what you noticed in the example with the idea below.

Remember

count() counts non-overlapping occurrences of a substring. Optional start and end bounds restrict the search.

Watch out for this idea

count() counts overlapping matches.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →