Courseiva
Strings →hardMultiple Choice

PCAP Strings Practice Question

Exhibit

$ python -c "import re; print(re.split(r'\s+', 'one   two three'))"

Refer to the exhibit. A Python script uses re.split with a regex pattern. What is the output?

⚠ Common exam trap

The PCAP exam often tests the distinction between splitting on a single character vs. a regex quantifier like `+`, leading candidates to mistakenly think each space produces a separate list element or that trailing delimiters always produce empty strings.

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

✓

['one', 'two', 'three']

`re.split(r'\s+', 'one two three')` splits the string on one or more whitespace characters (`\s+`). The pattern consumes all contiguous whitespace as a single delimiter, producing a list of the three non-empty substrings: `['one', 'two', 'three']`. No trailing empty string is included because the string does not end with whitespace.

Answer analysis

Option-by-option breakdown

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

  • ✗

    ['one two three']

    Why it's wrong here

    This single-element list shows that re.split failed to find any delimiter matches. With r'\s+', even a single space would trigger a split, so this outcome implies the regex was not r'\s+' (or the pattern was applied to a different/empty string). Because re.split only splits where its pattern matches, no matches means the original string is the only element in the returned list.

  • ✗

    ['one', 'two', 'three', '']

    Why it's wrong here

    The final '' appears because re.split preserves text after the last delimiter; if the input ends with whitespace, an empty string is emitted after the final match. For the input 'one two three' with no trailing space, re.split(r'\s+', text) returns no empty entry. Unlike str.split(), re.split does not strip leading/trailing delimiters, so a trailing delimiter produces this artifact.

  • ✓

    ['one', 'two', 'three']

    Why this is correct

    This is the correct output: r'\s+' matches one or more whitespace characters as a single delimiter, so the spaces before and after words are consumed. With the input 'one two three', each word is separated by exactly one space, and there is no leading/trailing whitespace, yielding exactly three substrings. The regex engine advances past all matching whitespace, so no empty strings appear.

  • ✗

    ['one', ' ', 'two', ' ', 'three']

    Why it's wrong here

    This list contains the separator spaces as elements, which only happens if the regex has a capturing group—for example, re.split(r'( )', text)—because re.split then includes each captured delimiter in the output. Splitting on a single space without parentheses would return ['one', 'two', 'three'] for this input, not include the spaces. The + quantifier is not the cause; the problem is the capturing group around the literal space.

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 →

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.