Remove Leading Whitespace from String — lstrip() and strip()
Which TWO of the following can be used to remove leading whitespace (spaces, tabs, newlines) from a string? (Choose exactly 2 correct answers.)
Quick Answer
The correct answers are lstrip() and strip(), as both can remove leading whitespace from a string. The lstrip() method is specifically designed to strip only leading characters—spaces, tabs, and newlines—from the left side of a string, while strip() removes whitespace from both the beginning and the end, which necessarily includes the leading portion. On the Certified Associate Python Programmer PCAP exam, this question tests your precise understanding of Python’s string methods and their directional behavior; a common trap is confusing rstrip() (which removes trailing whitespace) with lstrip(), or assuming a generic trim() or clean() method exists in Python. To remember, think of the letter L in lstrip() as standing for “left” or “leading,” and strip() as the all-in-one cleaner that handles both ends.
⚠ Common exam trap
Candidates often confuse `rstrip()` with removing leading whitespace because of the 'r' prefix, or incorrectly assume `trim()` or `clean()` are valid Python methods.
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
✓
lstrip()
The `lstrip()` method removes all leading whitespace characters (spaces, tabs, newlines) from the left side of a string. `strip()` removes leading and trailing whitespace, so it also satisfies the requirement of removing leading whitespace. Both are built-in string methods in Python.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
rstrip()
Why it's wrong here
rstrip() removes trailing whitespace from the right-hand end, leaving leading spaces, tabs and newlines untouched. It is tempting because it is a genuine whitespace-stripping method; lstrip() is the correct counterpart when the requirement is specifically to strip characters from the start.
- ✓
lstrip()
Why this is correct
lstrip() returns a copy with leading whitespace characters removed, leaving trailing and internal whitespace untouched. This matches the stem's constraint precisely, since only leading spaces, tabs and newlines must be stripped, and lstrip() is a built-in string method requiring no import.
- ✗
trim()
Why it's wrong here
trim() strips whitespace from both ends, so it also removes trailing characters, not leading ones exclusively. It is tempting because it is the obvious whitespace-removal method in many languages; lstrip() would be the correct Python choice when only leading whitespace must go.
- ✗
clean()
Why it's wrong here
clean() is not a Python string method, so calling it raises AttributeError rather than removing anything. It is tempting because the name suggests tidying input; strip(), lstrip() or a regular expression substitution would be the correct approach for leading whitespace.
- ✓
strip()
Why this is correct
strip() removes whitespace from both ends of the string, which necessarily includes all leading spaces, tabs and newlines. This satisfies the stem's requirement, since removing leading whitespace is a subset of its behaviour, and it is a built-in string method.
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 →
Same concept, more angles
1 more way this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Which two methods can be used to remove leading whitespace from a string? (Choose two.)
medium- ✓ A.s.strip()
- ✓ B.s.lstrip()
- C.s.split()
- D.s.chomp()
- E.s.rstrip()
Why A: `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.
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.