PCAP Strings Practice Question
A developer is building a configuration parser. They have a string `line = " key = value "` and need to remove the leading and trailing spaces, then split the line at the equals sign into a list of exactly two elements: the key and the value, with any spaces around the equals sign removed. Which expression achieves this?
⚠ Common exam trap
The trap here is assuming that stripping the whole line before splitting is enough to remove spaces around the delimiter, when in fact those spaces remain attached to the resulting substrings.
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
✓
[part.strip() for part in line.split('=')]
The goal is to remove whitespace around both the key and the value after splitting. Splitting first on the equals sign isolates the two components, and then stripping each component individually removes spaces that surround the equals sign. The list comprehension does this cleanly and preserves any internal spaces that belong to the key or value, making it the correct approach.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
[part.strip() for part in line.split('=')]
Why this is correct
Splitting the original string on '=' gives [' key ', ' value ']. The list comprehension then applies .strip() to each part, removing all leading and trailing whitespace from both the key and the value. This yields ['key', 'value'], exactly two elements with spaces around the equals sign removed, satisfying the requirement.
- ✗
line.strip().split('=')
Why it's wrong here
This strips only the outer spaces, yielding 'key = value'. Splitting on '=' produces ['key ', ' value'] because the spaces around the equals sign remain attached to the key and value. The developer would still need to strip each element individually, so this does not meet the requirement of removing spaces around the equals sign.
- ✗
line.replace(' ', '').split('=')
Why it's wrong here
Replacing all spaces with empty strings turns ' key = value ' into 'key=value'. Splitting on '=' then yields ['key', 'value']. However, this approach also removes any spaces that might legitimately be part of the key or value (e.g., a multi-word value), which is not desired in a general configuration parser. It is too aggressive and not equivalent to stripping only the surrounding whitespace.
- ✗
line.strip(' =').split('=')
Why it's wrong here
The .strip(' =') call removes any leading and trailing characters that are either space or equals sign. On ' key = value ', it would strip the leading spaces and the trailing spaces, but it would not remove the spaces around the internal equals sign. The result of split('=') is still ['key ', ' value'], so the key and value retain unwanted spaces.
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 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.