Courseiva
StringsmediumMultiple ChoiceObjective-mapped

Using count() to Find Occurrences of a Substring in Python

A developer needs to count the number of occurrences of the substring 'is' in the string 'This is a test. Is this a test?'. Which code correctly performs the count?

Quick Answer

The correct answer is 'This is a test. Is this a test?'.count('is') because Python's string method count(substring) returns the number of non-overlapping occurrences of the substring within the string, and here it correctly returns 2, counting the 'is' in 'This' and the standalone 'is', while ignoring the capitalized 'Is' since the method is case-sensitive. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of string methods and the common trap of forgetting that count() does not perform case-insensitive matching by default—a frequent source of off-by-one errors. To count substring occurrences in Python effectively, remember that count() scans left to right and only counts non-overlapping matches, so overlapping patterns like 'aaa' in 'aaaa' would yield 2, not 3. A handy memory tip: count() is case-conscious, not case-blind—capital letters are invisible to it.

⚠ Common exam trap

Python Institute often tests the distinction between string methods that return indices (`find`, `index`) versus those that return counts (`count`), and the trap here is that candidates confuse `count()` with `find()` or `index()`, or incorrectly assume `split().count()` works for substring counting.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

'This is a test. Is this a test?'.count('is')

Python's string method `count(substring)` returns the number of non-overlapping occurrences of the substring in the string. In 'This is a test. Is this a test?', 'is' appears twice (in 'This' and 'is'), and the method counts them correctly, ignoring case sensitivity (the capitalized 'Is' is not counted).

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • 'This is a test. Is this a test?'.split().count('is')

    Why it's wrong here

    Splitting yields words, 'is' appears only once as a whole word.

  • 'This is a test. Is this a test?'.count('is')

    Why this is correct

    Correctly counts overlapping? No, count does not count overlapping, but 'is' appears at positions 5 and 17, not overlapping, so returns 2.

  • 'This is a test. Is this a test?'.index('is')

    Why it's wrong here

    index() returns the index of first occurrence or raises error.

  • 'This is a test. Is this a test?'.find('is')

    Why it's wrong here

    find() returns the index of first occurrence, not count.

About these practice questions

One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. What is the output of 'hello'.count('l')?

easy
  • A.1
  • B.3
  • C.0
  • D.2

Why D: The string method `count('l')` returns the number of non-overlapping occurrences of the substring `'l'` in the string `'hello'`. The string `'hello'` contains the character `'l'` at indices 2 and 3, so the count is 2. Therefore, option D is correct.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.