Extract File Extension with rsplit
A developer needs to extract the file extension from a filename like 'document.pdf'. Which expression returns 'pdf'?
Quick Answer
The answer is filename.rsplit('.', 1)[-1] because rsplit performs a right-to-left split, isolating the last dot and returning the extension as the final element. This method is essential when filenames contain multiple dots, such as 'archive.tar.gz', where a standard split('.') would break on every dot and fail to correctly extract only the final extension. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of string methods and indexing, specifically how rsplit with a maxsplit argument prevents common errors from multiple delimiters. A frequent trap is assuming split('.')[-1] works universally, but it returns the wrong segment when dots appear earlier in the name. To remember: think of rsplit as "reverse split" — it starts from the right, so you always grab the last piece. Memory tip: "Right split, right result" — use rsplit when you only want the final extension.
⚠ Common exam trap
The PCAP exam often tests the misconception that `split('.')[1]` is safe for extracting extensions, but the trap is that it fails for filenames with multiple dots or no dot, whereas `rsplit` with maxsplit handles these edge cases correctly.
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
✓
filename.rsplit('.', 1)[-1]
`rsplit('.', 1)[-1]` splits the string from the right at the last occurrence of the dot, limiting to one split, and then retrieves the last element (index -1), which is the file extension. This handles filenames with multiple dots (e.g., 'archive.tar.gz') correctly, returning only the final extension.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
filename.split('.')[1]
Why it's wrong here
Works only if there is exactly one dot; fails for multiple dots.
- ✗
filename.split('.')[0]
Why it's wrong here
Returns the part before the first dot, not the extension.
- ✓
filename.rsplit('.', 1)[-1]
Why this is correct
Splits from right at the last dot, returning the extension correctly.
- ✗
filename[-3:]
Why it's wrong here
Assumes extension is exactly 3 characters; fails for '.ini' or '.jpeg'.
Go deeper
Related to this question
About these practice questions
One of 169 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. A developer needs to extract the file extension from a string like 'report.pdf'. Which string method is most appropriate?
easy- A.str.find('.')
- ✓ B.str.split('.')[-1]
- C.str.partition('.')[2]
- D.str.rstrip('.pdf')
Why B: `str.split('.')[-1]` splits the string at each dot and returns the last element, which is the file extension. This method works reliably for simple cases like 'report.pdf' and is a common Python idiom for extracting extensions.
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.