Courseiva
StringseasyMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A developer wants to check if a string ends with a specific suffix. Which method should be used?

⚠ Common exam trap

Python Institute often tests the distinction between `endswith()` and `startswith()`, trapping candidates who confuse prefix and suffix checks, or who mistakenly use `find()` or `index()` which locate substrings anywhere in the string rather than at the end.

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

endswith()

The `endswith()` method is specifically designed to check if a string ends with a given suffix, returning a boolean value. This is the correct and most direct approach for the task described, as it avoids manual slicing or comparison.

Answer analysis

Option-by-option breakdown

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

  • endswith()

    Why this is correct

    The `endswith()` method is the dedicated predicate for suffix testing: it returns `True` only when the final characters of the string exactly match the given suffix, and `False` otherwise. It also accepts optional `start`/`end` slice arguments, which allow you to check only a portion of the string, and it performs a case-sensitive comparison by default (use `casefold()` or lowercasing for case-insensitive checks). Because it returns a boolean directly, it cleanly satisfies the developer's requirement to verify whether the string ends with a specific substring.

  • index()

    Why it's wrong here

    The `index()` method searches for a substring and returns the lowest index where it begins, raising a `ValueError` if not found; it does not anchor the search to the end of the string, so it cannot verify a suffix. It is tempting because it can locate the suffix’s position, but it would only be correct if the developer needed to find any occurrence of a substring, not to test for a trailing match.

  • find()

    Why it's wrong here

    The `find()` method returns the lowest index at which a substring occurs, or `-1` if it is not present, so its result is an integer, not a boolean; a common misuse is `s.find('suffix') != -1`, which only tells you the substring exists somewhere, not that it is a trailing match. Even if you manually compare the found index to `len(s) - len('suffix')`, `find()` may have located an earlier occurrence that is not the final one, and it does not anchor behavior to the end of the string because it searches from the beginning. It is a general-purpose positional search method, and attempting to force it into a suffix predicate requires extra arithmetic and error-prone manual verification, making it the wrong tool for this specific check.

  • startswith()

    Why it's wrong here

    The `startswith()` method tests whether a string begins with a given prefix, comparing characters at index `0` rather than inspecting the end of the string. Because it is anchored to the front, it cannot verify a suffix condition; a string may start with the same characters it ends with, but the method's semantics do not guarantee a match at the final position. Python intentionally provides separate predicates for prefix and suffix tests — `startswith()` for the beginning and `endswith()` for the end — so using `startswith()` to answer an 'ends with' question is a clear semantic mismatch that would yield incorrect results for strings where the prefix and suffix differ.

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

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.