Courseiva
StringsmediumMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

You are a developer for an e-commerce platform. The system receives product descriptions from suppliers in various formats. One supplier sends descriptions with inconsistent capitalization, extra whitespace, and occasional leading/trailing punctuation. Your task is to write a function that normalizes these descriptions: convert to lowercase, remove leading/trailing whitespace and punctuation (.,!?;:), and replace multiple spaces with a single space. The function should return the cleaned string. Which implementation correctly performs all these steps?

⚠ Common exam trap

Python Institute often tests the order of operations in string normalization, and the trap here is that candidates may think `strip()` with a punctuation argument also handles whitespace or that `split()` and `join()` alone are sufficient to remove punctuation, leading them to choose options that miss one or more required steps.

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

def normalize(s): import re; s = s.strip(); s = s.strip('.,!?;:'); s = s.lower(); s = re.sub(r'\s+', ' ', s); return s

It performs all required steps in the correct order: it first strips leading/trailing whitespace with `strip()`, then removes leading/trailing punctuation using `strip('.,!?;:')`, converts to lowercase with `lower()`, and finally replaces multiple spaces with a single space using `re.sub(r'\s+', ' ', s)`. This ensures that punctuation is removed only from the edges after whitespace is handled, and internal whitespace is normalized last.

Answer analysis

Option-by-option breakdown

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

  • def normalize(s): import re; s = s.strip(); s = s.strip('.,!?;:'); s = s.lower(); s = re.sub(r'\s+', ' ', s); return s

    Why this is correct

    The correct implementation first trims surrounding whitespace with s.strip(), then removes any leading/trailing punctuation characters via s.strip('.,!?;:') — a subtle but important order, because punctuation attached after spaces (e.g., " hello! ") is only exposed for removal after the outer whitespace is gone. Lowercasing follows, and finally re.sub(r'\s+', ' ', s) collapses any runs of internal whitespace (tabs, newlines, multiple spaces) into a single space. This sequence yields a fully canonical form: " Hello, World!! " becomes "hello, world". It deliberately handles each normalization dimension independently, making the result predictable for exact-match comparisons.

  • def normalize(s): return ' '.join(s.lower().split())

    Why it's wrong here

    This concise idiom ' '.join(s.lower().split()) collapses all whitespace runs into single spaces and trims outer whitespace, but it never removes punctuation. The .split() call with no arguments splits on any whitespace and discards empty tokens, so " Hello, World! " becomes 'hello, world!' — the comma and exclamation mark remain attached to their words. Because the question's normalization goal explicitly includes removing punctuation, this implementation leaves leading/trailing punctuation like commas and periods intact, failing the required behavior for order IDs or product codes that may carry stray punctuation.

  • def normalize(s): return s.lower().strip('.,!?;: ')

    Why it's wrong here

    Here s.lower() lowercases first, then .strip('.,!?;: ') removes any combination of the listed punctuation and spaces from both ends. This does strip leading/trailing punctuation and spaces in one step, but it does not collapse multiple internal spaces: "Hello, World" would remain "hello, world" because .strip() only examines the boundaries, never the middle. The included space character in the strip set is misleading; it only affects edge trimming, not interior whitespace. So while punctuation is handled, the whitespace-collapse requirement is left unsatisfied, making this version only partially normalized.

  • def normalize(s): return s.strip().lower()

    Why it's wrong here

    s.strip().lower() removes only leading and trailing whitespace (default behavior of .strip()) and then lowercases the string. It leaves all punctuation untouched — both internal and at the edges — because no punctuation characters are ever passed to strip. Internal double spaces also survive, since .strip() does not alter the interior of the string. For example, " Mail, ITEM#42! " becomes "mail, item#42!" — neither punctuation removal nor whitespace collapsing happens, so this snippet fails three of the four required transformations and is clearly incomplete.

About these practice questions

One of 169 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 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.