PCAP Strings Practice Question
Which two methods can be used to remove leading whitespace from a string? (Choose two.)
⚠ Common exam trap
The trap here is that candidates may incorrectly consider `s.strip()` as the method for removing only leading whitespace, overlooking that `lstrip()` is the precise method. Additionally, `chomp()` is not a Python string method; it is from Ruby, and `split()` and `rstrip()` do not remove leading whitespace.
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()
`s.strip()` removes both leading and trailing whitespace from the string, including spaces, tabs, and newline characters. This method is commonly used when you need to clean up a string entirely, but it does more than just leading whitespace removal.
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.strip()
Why this is correct
Calling s.strip() returns a new string with all leading and trailing whitespace removed, so leading whitespace is indeed eliminated as part of the process. This makes it a valid choice for the task, even though it also strips the right end. By default, strip() removes spaces, tabs, and newline characters; an optional chars argument can narrow the set.
- ✓
s.lstrip()
Why this is correct
The lstrip() method returns a copy of the string with only the leading whitespace removed, leaving the trailing end untouched. It is the most direct and precise method for removing leading whitespace, as it scans from the left and stops when a non-whitespace character is encountered. Like strip(), it can take an optional argument specifying a set of characters to remove instead of the default whitespace.
- ✗
s.split()
Why it's wrong here
The split() method separates a string into a list of substrings based on a delimiter, defaulting to any whitespace run. It does not return a string with whitespace removed; instead, it discards the delimiter and returns a list, often leaving empty strings if leading delimiters exist when a specific separator is given. Therefore, calling s.split() changes the data type and cannot serve as a string-trimming method for leading whitespace.
- ✗
s.chomp()
Why it's wrong here
The chomp() method comes from Ruby and Perl, not Python; Python's standard string type does not define it. Attempting to call s.chomp() raises an AttributeError because the str class lacks that method. To strip leading whitespace in Python, one must use lstrip() or strip(), not a nonexistent chomp() method.
- ✗
s.rstrip()
Why it's wrong here
The rstrip() method removes whitespace only from the right end of the string, that is, the trailing side. Since it does not touch the left side, leading whitespace remains intact, so it fails to satisfy the requirement. It is a directional counterpart to lstrip(), and while it may be useful for trimming line endings, it is not appropriate for this task.
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.