PCAP Strings Practice Question
You are a developer at a company that processes customer feedback. Each feedback entry is stored as a string containing a rating (1-5) followed by a colon and then the comment. For example: '4: Great service'. You need to extract only the comments from feedback that have a rating of 4 or 5. You have a list of feedback strings. Which code snippet correctly implements this?
⚠ Common exam trap
Python Institute often tests the difference between substring matching (using 'in') and exact prefix matching (using startswith or split-based comparison), leading candidates to choose Option D because they overlook that '4' or '5' could appear anywhere in the string, not just as the rating.
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
✓
[s.split(':')[1].strip() for s in feedback if s.split(':')[0].strip() in ('4','5')]
It splits each feedback string on ':', extracts the comment (index [1]), strips whitespace, and filters only those entries where the rating (index [0], stripped) is exactly '4' or '5'. This ensures only comments from high-rated feedback are collected, handling potential spaces around the colon.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
[s for s in feedback if s.startswith('4') or s.startswith('5')]
Why it's wrong here
This comprehension selects entire feedback strings rather than extracting the comment portion, so it cannot return comments. The startswith() check also fails to isolate the rating precisely: a rating like '45' would pass because it starts with '4', and any leading whitespace would cause a valid rating to be ignored. It neither splits on the delimiter nor verifies the rating is exactly one of the allowed values.
- ✗
[s.split(':') for s in feedback][1]
Why it's wrong here
This is a list comprehension that splits each item, producing a list of lists; the trailing [1] then indexes the outer list, not the second element of each split string. Thus it would return an entire split result (or raise an IndexError if the list is short) instead of the comment for every feedback. Additionally, there is no rating filter at all, so the expression is fundamentally unrelated to the task.
- ✓
[s.split(':')[1].strip() for s in feedback if s.split(':')[0].strip() in ('4','5')]
Why this is correct
This is correct because it first splits the feedback string at the colon, strips whitespace from both the rating and comment, and only keeps the comment when the rating is exactly '4' or '5'. The condition uses a tuple membership test on the stripped first part, avoiding false positives from comments that merely contain the digit. It cleanly returns the comment portion, which is exactly what the requirement asks for.
- ✗
[s.split(':')[1] for s in feedback if '4' in s or '5' in s]
Why it's wrong here
This filters on whether '4' or '5' appears anywhere in the entire feedback string, rather than checking the rating field specifically. As a result, a comment like 'I would not pay 5 dollars' would be included even if the rating is 1, and the extracted comment is not stripped, leaving unwanted whitespace. The correct logic must compare the first field to the allowed ratings, not probe the whole string for digits.
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.