PCAP Strings Practice Question
A log processing script receives a multiline string log. The script needs to check if the string ends with the substring 'ERROR'. Which method should be used?
⚠ Common exam trap
The PCAP exam often tests the distinction between checking substring presence anywhere versus at a specific position, and candidates mistakenly choose `in` or `find` because they think 'checking if it ends with' is equivalent to 'checking if it contains'.
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
✓
log.endswith('ERROR')
The `endswith()` method is specifically designed to check if a string ends with a given substring. It returns `True` if the string ends with 'ERROR', making it the most direct and readable solution for this requirement.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
log.find('ERROR') != -1
Why it's wrong here
The str.find() method returns the lowest index at which the substring 'ERROR' occurs, or -1 if it is absent. The condition != -1 therefore merely confirms that 'ERROR' appears somewhere in the log, whether at the beginning, middle, or end. It says nothing about the trailing portion of the string, so a log that contains 'ERROR' in a middle line would incorrectly pass this test.
- ✗
log.rfind('ERROR') == len(log)-5
Why it's wrong here
Using log.rfind('ERROR') == len(log)-5 attempts to check that the last occurrence of 'ERROR' starts exactly five characters from the end. This is brittle because it hard-codes the substring length and assumes the string has no trailing newline, carriage return, or spaces—common in real log files. Any such trailing characters shift the index, causing a false negative even when the log does end with 'ERROR'. It is also non-idiomatic and obscures intent compared with the dedicated endswith() method.
- ✗
'ERROR' in log
Why it's wrong here
The expression 'ERROR' in log is a substring membership test, returning True whenever the exact sequence appears anywhere in the string. It completely ignores positional information, so a log that mentions 'ERROR' in a middle line or as part of a larger error message satisfies the condition. This makes the check far too permissive for verifying that the final line ends with the error marker.
- ✓
log.endswith('ERROR')
Why this is correct
The str.endswith('ERROR') method is the idiomatic and precise way to test whether a string ends with the given suffix. It performs a direct comparison of the final characters and returns True only if the last five characters are exactly 'ERROR', with no need for manual indexing or length arithmetic. This is the clear, readable solution that handles trailing content correctly and is the standard approach in Python.
Go deeper
Related to this question
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 →
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.