PCAP Strings Practice Question
A developer wants to remove all leading and trailing whitespace from a string, but preserve internal spaces. Which line of code accomplishes this?
⚠ Common exam trap
Python Institute often tests the distinction between `strip()`, `lstrip()`, and `rstrip()`, and the trap here is that candidates may confuse `strip()` with `replace(' ', '')` or think that `lstrip()` alone is sufficient, failing to recognize that `strip()` handles both ends in one call.
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
✓
s = s.strip()
`s.strip()` removes all leading and trailing whitespace characters (spaces, tabs, newlines) from the string while preserving internal spaces. This is the exact requirement: eliminate whitespace at the boundaries only, leaving the internal content unchanged.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
s = s.lstrip()
Why it's wrong here
The lstrip() method returns a new string with only leading whitespace removed, while any whitespace at the end of the string remains untouched. Since the requirement explicitly demands removal of trailing whitespace as well, this approach fails to fully trim the string and is therefore incorrect.
- ✗
s = s.strip().lstrip()
Why it's wrong here
Calling strip() first already eliminates both leading and trailing whitespace, and the subsequent lstrip() has nothing left to remove, so the chained call is entirely redundant and adds no functional value. While the final result may be correct, this over-engineered solution obscures intent and wastes a method call, making it an inappropriate answer to a simple trimming task.
- ✗
s = s.replace(' ', '')
Why it's wrong here
The replace(' ', '') call removes every ASCII space character from the entire string, not just those at the boundaries, so internal spaces between words are lost. Furthermore, it only targets literal space characters, ignoring other whitespace such as tabs and newlines, so it neither trims properly nor preserves meaningful formatting.
- ✓
s = s.strip()
Why this is correct
The strip() method returns a copy of the string with all leading and trailing whitespace removed, using the standard set of whitespace characters including space, tab, and newline, while leaving internal spaces intact. This precisely satisfies the requirement, making it the correct and idiomatic way to trim a string in Python.
Go deeper
Related to this question
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 →
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.