Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

A developer is cleaning data from a CSV file. They have a string `record = "John,Doe,35,Engineer"` and want to replace all commas with semicolons. However, they also want to ensure that any leading or trailing whitespace in the entire string is removed before the replacement. Which expression produces the desired result?

⚠ Common exam trap

The trap here is assuming that strip() removes commas or that replacing spaces globally is equivalent to stripping only the edges.

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

✓

record.strip().replace(',', ';')

The correct approach is to strip leading and trailing whitespace first, then replace commas with semicolons. Using strip() before replace ensures that any surrounding whitespace is removed without affecting internal spaces. The other options either remove the wrong characters, remove all spaces, or perform the operations in a different order that might not align with the stated requirement, though in this specific case some yield the same output. However, the requirement explicitly states 'before the replacement', making the strip-then-replace order the intended one.

Answer analysis

Option-by-option breakdown

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

  • ✗

    record.strip(',').replace(',', ';')

    Why it's wrong here

    The .strip(',') call removes leading and trailing commas, not whitespace. Since the string has no leading or trailing commas, it does nothing. Then .replace(',', ';') replaces all commas with semicolons, resulting in 'John;Doe;35;Engineer'. However, if there were leading or trailing spaces, they would not be removed, so this fails the requirement of removing whitespace.

  • ✗

    record.replace(',', ';').strip()

    Why it's wrong here

    This expression replaces commas first, then strips whitespace. While it would also remove leading and trailing whitespace, the order is reversed. The problem does not specify an order issue, but in some cases, if the replacement introduced whitespace (which it does not here), stripping after might be necessary. However, for this scenario, both orders yield the same result if there is no whitespace. But the question asks for the expression that ensures leading/trailing whitespace is removed before replacement. Since replace does not introduce whitespace, this is also correct in practice, but the intended order is strip then replace. In a strict sense, both A and B produce the same output for the given input, but the requirement says 'before the replacement', so A is the one that explicitly does that.

  • ✓

    record.strip().replace(',', ';')

    Why this is correct

    The expression first calls .strip() on the record, removing any leading and trailing whitespace. Then .replace(',', ';') replaces every comma with a semicolon. For the given string without extra whitespace, it yields 'John;Doe;35;Engineer'. If there were leading or trailing spaces, they would be removed before replacement. This meets both requirements in the correct order.

  • ✗

    record.replace(' ', '').replace(',', ';')

    Why it's wrong here

    This removes all spaces from the string, not just leading and trailing ones. For 'John,Doe,35,Engineer', there are no spaces, so it would yield the same as A. But if the data contained spaces within fields (e.g., 'John, Doe, 35, Engineer'), those internal spaces would also be removed, which is not desired. The requirement is only to remove leading and trailing whitespace, so this is too aggressive.

About these practice questions

One of 421 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 and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.