PCAP Strings Practice Question
What is the output of the following code? s = 'Hello'; print(s.find('l'))
⚠ Common exam trap
PCAP often tests the distinction between 0-based and 1-based indexing, and the fact that `find()` returns the first occurrence, not the last or any subsequent one.
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 `str.find()` method returns the lowest index of the first occurrence of the substring. In the string 'Hello', the character 'l' first appears at index 2 (0-based indexing: H=0, e=1, l=2). Therefore, `s.find('l')` returns 2, making option B 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
Option 1 is incorrect because Python uses zero-based indexing, so index 1 in the string 'hello' refers to the second character, which is 'e', not the letter 'l'. The str.index() method searches for the first occurrence of the specified substring and returns its exact position; since 'e' appears at index 1, this cannot be the result when searching for 'l'.
- ✓
2
Why this is correct
Option 2 is correct: the string 'hello' has the character positions h(0), e(1), l(2), l(3), and o(4). When str.index('l') is called, Python scans the string left-to-right and returns the smallest index where the substring 'l' occurs, which is 2. Therefore the code prints the integer 2.
- ✗
0
Why it's wrong here
Option 3 is incorrect because index 0 points to the very first character of the string, which in 'hello' is the lowercase 'h' (or 'H' if the string were capitalized). Zero-based indexing means index 0 does not correspond to any 'l', and since the method searches specifically for 'l', the returned value cannot be 0.
- ✗
3
Why it's wrong here
Option 4 is incorrect because although 'hello' does contain an 'l' at index 3, that is the second occurrence of the letter. The str.index() method always returns the first occurrence of the target substring; to get the last occurrence you would need str.rindex(), while a plain index() call stops at the leftmost match. Thus index 3 does not match the output.
Go deeper
Related to this question
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 →
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.