PCAP Strings Practice Question
A developer needs to extract the file extension from a string like 'report.pdf'. Which string method is most appropriate?
⚠ Common exam trap
Python Institute often tests the distinction between `partition()` and `split()` — candidates mistakenly choose `partition()` because it seems simpler, but they overlook that `partition()` only splits on the first occurrence, making it unsuitable for extensions in filenames with multiple dots.
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
✓
str.split('.')[-1]
`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.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
str.find('.')
Why it's wrong here
str.find('.') returns an integer index of the first occurrence of the dot within the string, not the substring that follows it. Since the goal is to obtain the extension itself, an integer position is useless without additional slicing. Moreover, find() only locates the first dot, not the last, so it would fail to capture the correct extension for names containing multiple dots.
- ✓
str.split('.')[-1]
Why this is correct
str.split('.')[-1] is the correct idiom because it divides the entire string on every dot and then uses -1 to select the final element, which is exactly the substring after the last period. This handles file names with multiple dots, returning the last component as the extension. One caveat is that if no dot exists, the whole original string is returned, but for typical extension extraction this is acceptable and widely used.
- ✗
str.partition('.')[2]
Why it's wrong here
str.partition('.')[2] splits the string at the first dot only, returning the portion after that single separator. For a name like 'archive.tar.gz', this yields 'tar.gz' rather than 'gz', incorrectly including an intermediate segment. Additionally, when no dot is present, partition returns an empty string for index [2], whereas split would return the original name, so the failure modes differ.
- ✗
str.rstrip('.pdf')
Why it's wrong here
str.rstrip('.pdf') does not extract an extension; it removes any trailing characters that belong to the set {'.', 'p', 'd', 'f'}. This means it deletes all matching characters from the end, not just the literal suffix '.pdf', so a file named 'report.pdf' could lose more than intended if it ends with extra characters. It also returns the remaining part of the string, not the extension, making it fundamentally unsuited for extraction.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.