PCAP Strings Practice Question
A developer wants to remove leading and trailing whitespace from a string. Which method should be used?
⚠ 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 the non-existent `trim()` method from other languages, or think `lstrip()` or `rstrip()` alone suffice for full trimming.
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.strip()
The `strip()` method in Python removes both leading and trailing whitespace (including spaces, tabs, and newlines) from a string. This is the standard method for trimming whitespace from both ends, as specified in Python's string documentation.
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.lstrip()
Why it's wrong here
s.lstrip() only removes leading whitespace, leaving the trailing whitespace intact. For example, ' hello '.lstrip() returns 'hello ', which still has spaces at the end. Therefore it does not satisfy the requirement of removing whitespace from both ends of the string.
- ✗
s.trim()
Why it's wrong here
s.trim() is invalid in Python because the string type does not define a method named trim(). Calling s.trim() raises an AttributeError, since Python uses the strip() method for removing leading and trailing whitespace. This option is a common mistake for developers coming from languages like Java or JavaScript, where trim() exists.
- ✗
s.rstrip()
Why it's wrong here
s.rstrip() only removes trailing whitespace, leaving the leading whitespace unchanged. For instance, ' hello '.rstrip() returns ' hello', which still contains spaces at the beginning. It is the mirror-image counterpart to lstrip(), and also fails to handle both ends of the string.
- ✓
s.strip()
Why this is correct
s.strip() correctly removes whitespace from both the beginning and the end of the string. For example, ' hello '.strip() returns 'hello', eliminating the leading and trailing spaces. Because it precisely matches the requirement, and because strings are immutable, it returns a new stripped string rather than modifying the original.
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.