PCAP Strings Practice Question
A junior developer is writing a script that processes user input. The script reads a line of text from the console and needs to remove any leading or trailing whitespace. The developer uses the strip() method but notices that it also removes other characters like newline. However, the requirement is to remove only spaces (not tabs or newlines). Which course of action should the developer take to remove only leading and trailing spaces?
⚠ Common exam trap
Many candidates assume strip() without arguments only removes spaces, but the PCAP exam tests the nuance that strip() by default removes all whitespace characters, and that passing a specific character as an argument restricts the removal to that character only.
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 strip(' ') with a space argument
The strip() method in Python, when called with no arguments, removes all leading and trailing whitespace characters, including spaces, tabs, and newlines. By passing a space character as the argument, strip(' '), the method is instructed to remove only that specific character (space) from the ends of the string, leaving tabs and newlines intact. This directly meets the requirement to remove only leading and trailing spaces.
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 replace(' ', '') on the string
Why it's wrong here
replace(' ', '') deletes every space in the string, including interior ones, rather than only leading and trailing spaces. It is tempting as a targeted character removal, but it corrupts multi-word input; strip(' ') is the method that trims only the space character at both ends.
- ✗
Use lstrip() and rstrip() with no arguments
Why it's wrong here
lstrip() and rstrip() with no arguments strip all whitespace characters, including tabs and newlines, exactly as strip() does, so the requirement to remove only spaces remains unmet. They are tempting because they let you target one side at a time, which suits trimming a single end, but not restricting the character set.
- ✗
Use split() and join()
Why it's wrong here
split() with no argument splits on any whitespace run and discards empty tokens, so joining cannot restore internal spacing or preserve tabs and newlines. It is tempting for trimming, but it collapses interior whitespace and removes the very characters the requirement says to keep.
- ✓
Use strip(' ') with a space argument
Why this is correct
Passing a space as the argument restricts strip() to that character set only, so leading and trailing spaces are removed while tabs, newlines and other whitespace remain intact. The bare strip() call would strip all whitespace types, violating the stated requirement.
Go deeper
Related to this question
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 →
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.