Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

A developer is writing a function to reverse each word in a sentence while preserving the original order of the words. For example, 'Hello World' should become 'olleH dlroW'. The current implementation is: def reverse_words(s): return ' '.join(word[::-1] for word in s.split()). This works for simple cases. However, the input may contain multiple spaces between words (e.g., 'Hello World') and tabs. The requirement is to preserve the exact whitespace between words (including tabs and multiple spaces) in the output. Which of the following modifications will achieve this while keeping the approach efficient?

⚠ Common exam trap

The Python Institute often tests the distinction between `str.split()` (which collapses all whitespace) and `re.split` with a capturing group (which preserves whitespace), and the trap here is assuming that simple `split` and `join` can handle arbitrary whitespace patterns without losing information.

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

✓

Use re.split(r'(\s+)', s) to separate words and whitespace, then reverse the word parts and join back

`re.split(r'(\s+)', s)` splits the string into a list where every other element is a whitespace segment (preserving exact spaces and tabs), and the word segments can be reversed individually. Joining the list back with `''.join(...)` reconstructs the original whitespace exactly, meeting the requirement efficiently without manual iteration.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Use s.split(' ') and handle empty strings to preserve spaces, but ignore tabs

    Why it's wrong here

    Splitting on a single space still collapses runs of spaces into empty strings and leaves tabs embedded inside tokens, so exact whitespace is not preserved. It is tempting because split(' ') is the obvious minimal edit, and would work if inputs contained only single spaces and no tabs.

  • ✗

    Iterate over the string, detect word boundaries, and build a new string manually

    Why it's wrong here

    Manual boundary detection and string concatenation rebuilds the result correctly but is not the efficient approach the stem requires, since repeated concatenation and character-level scanning add overhead. It is tempting because it gives full control over whitespace, and would be correct if no regex-based alternative existed.

  • ✓

    Use re.split(r'(\s+)', s) to separate words and whitespace, then reverse the word parts and join back

    Why this is correct

    Splitting on whitespace collapses runs of spaces and tabs, losing the original separators. Capturing the whitespace with re.split(r'(\s+)') keeps those groups in the list, so reversing only the word elements and rejoining preserves exact spacing.

  • ✗

    Use s.split() and then join with the original spaces using a separate loop that tracks whitespace

    Why it's wrong here

    s.split() discards all original whitespace, so a separate loop cannot recover the exact tabs and multiple spaces that were already lost. It is tempting because it appears to track whitespace explicitly, and would be correct if the original separators were retained before splitting rather than discarded.

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 →

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.