Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

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

⚠ Common exam trap

Python Institute often tests the `count()` method with a single character substring to see if candidates correctly count occurrences, but the trap here is that some candidates might mistakenly count the total number of characters or misremember the string `'hello'` as having only one `'l'`.

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

✓

2

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.

Answer analysis

Option-by-option breakdown

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

  • ✗

    1

    Why it's wrong here

    The string 'hello' contains two lowercase 'l' characters, located at the third and fourth positions when using 1-based indexing. A count of 1 is an undercount; it would only be correct if one of the two 'l's were removed or if the string were something like 'helo'. Python's str.count('l') method scans the entire string and returns the total number of matches, which is 2, not 1.

  • ✗

    3

    Why it's wrong here

    A value of 3 is an overcount for 'hello'.count('l') because the word 'hello' has exactly two 'l' characters, not three. This incorrect result might arise from mistakenly including the 'l' in a different word in the full phrase 'hello count l' or from counting characters outside the target string, but the method operates only on the specified string 'hello', which contains two occurrences.

  • ✗

    0

    Why it's wrong here

    Returning 0 would mean no 'l' characters are found, but the string 'hello' clearly contains two lowercase 'l's, at indices 2 and 3 in zero-based indexing. A zero result might come from confusing the character 'l' with a different symbol such as the digit '1' or uppercase 'I', or from searching for the wrong letter. The str.count() method is case-sensitive and would find both 'l's, so 0 is incorrect.

  • ✓

    2

    Why this is correct

    The expression 'hello'.count('l') correctly returns 2, the total number of non-overlapping occurrences of the character 'l' in the string. Python's str.count() method counts each match from left to right, and since 'hello' consists of 'h', 'e', 'l', 'l', 'o', it finds an 'l' at position 2 and another at position 3 (0-based). Therefore, the output is exactly 2.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.