PCAP Strings Practice Question
Which TWO of the following expressions evaluate to `True`? (Select exactly two.)
⚠ Common exam trap
Many exam-takers confuse the `in` operator with checking individual characters in any order, or mistakenly think that a longer substring can be found in a shorter string, leading them to select option E.
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
✓
'ab' in 'abc'
The `in` operator checks if the substring 'ab' appears anywhere within the string 'abc'. Since 'abc' contains the consecutive characters 'a' followed by 'b', the expression evaluates to True.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
'ab' in 'abc'
Why this is correct
Python's `in` operator for strings performs a substring containment test, checking whether the left operand appears as a contiguous sequence within the right operand. In 'abc', the two-character sequence 'ab' is present at indices 0 and 1, so the expression evaluates to True. This is exact, character-by-character matching with no wildcard or fuzzy semantics.
- ✗
'x' in 'abc'
Why it's wrong here
The expression `'x' in 'abc'` returns False because `in` checks for an exact character match at each position of the target string. Since 'abc' consists of the characters 'a', 'b', and 'c', none equals 'x', and the test fails. Importantly, string membership does not perform regular-expression matching or case-insensitive comparisons, so 'x' cannot match anything here.
- ✗
'ab' not in 'abc'
Why it's wrong here
Here `'ab' not in 'abc'` is False because the `not in` operator is the logical negation of `in`. Since `'ab' in 'abc'` is True—the substring 'ab' appears at the beginning of 'abc'—negating that result yields False. The expression is true only if the substring is entirely absent, which is not the case.
- ✓
'a' in 'abc'
Why this is correct
The expression `'a' in 'abc'` is True because the string 'a' is a substring of 'abc', occurring at index 0. In Python, a one-character string is treated as a substring, so the containment check succeeds if that single character appears anywhere in the target. Here the target starts with 'a', so the result is True.
- ✗
'abc' in 'ab'
Why it's wrong here
The expression `'abc' in 'ab'` cannot be True because Python's substring search requires the searched string to be no longer than the target. With target 'ab' of length 2, the 3-character sequence 'abc' cannot fit at any starting position. Even conceptually, the characters don't align: 'ab' lacks 'c', so containment fails.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.