PCAP Strings Practice Question
A developer needs to check if a filename starts with the prefix 'report_'. Which string method should be used?
⚠ Common exam trap
Python Institute often tests the exact naming of Python string methods, and the trap here is that candidates may confuse `startswith()` with similar-sounding but non-existent methods like `starts()` or `beginwith()`, or incorrectly assume a method like `prefix()` exists based on other programming languages.
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
✓
startswith()
The `startswith()` method is the correct string method in Python to check if a string begins with a specified prefix. It returns `True` if the string starts with the given substring, otherwise `False`, making it the exact tool for checking if a filename starts with 'report_'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
prefix()
Why it's wrong here
prefix() is not a valid Python string method. The str class does not expose a method named prefix() for testing string prefixes; calling it raises AttributeError. The correct approach is the dedicated str.startswith(prefix) method, which exists precisely for this purpose. This option fails because it conflates the concept of a prefix with a hypothetical method name that the standard library never defines.
- ✓
startswith()
Why this is correct
startswith() is the correct and idiomatic Python string method for checking whether a string begins with a given prefix. It accepts a single string, a tuple of strings, or optional start and end indices, returning True if the string starts with the specified prefix and False otherwise. Its behavior is case-sensitive, and it is implemented in C for efficiency, making it the standard tool for this common validation task. This is the only method among the options that actually exists and performs the required check.
- ✗
starts()
Why it's wrong here
starts() is not a valid Python string method. While its name resembles startswith(), Python does not define a starts() method on the str type, so any attempt to call filename.starts(prefix) raises AttributeError. The method name must be exactly startswith() to match the string API; partial or abbreviated names are never recognized. This option is incorrect because it assumes a shortened form that the language designers did not include.
- ✗
beginwith()
Why it's wrong here
beginwith() is not a valid Python string method. Although its meaning mirrors startswith(), the Python standard library does not provide a method with that name, and invoking it on a string raises AttributeError. The method name was chosen as startswith() during language design, and unlike some other languages, Python does not offer synonyms like beginwith for the same operation. This option is wrong because it relies on a non-existent alias rather than the actual API.
Go deeper
Related to this question
About these practice questions
One of 421 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.