PCAP Strings Practice Question
A data analyst is cleaning a CSV file. They have a string variable containing a row of data: 'John,Doe,30,New York'. They need to extract the last name 'Doe' using string methods. The analyst writes: name = row.split(',')[1]. However, they are concerned about performance because the file contains millions of rows. They want to use a more efficient method that extracts the substring without creating a full list. Which approach should the analyst use?
⚠ Common exam trap
A common trap in PCAP is thinking that any split() variant is always the best approach, ignoring the memory overhead of list creation in performance-critical scenarios.
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
✓
Use string slicing after finding the comma positions: start = row.find(',')+1; end = row.find(',', start); name = row[start:end]
It avoids creating a full list of all fields by using `find()` to locate the comma positions and then slicing the substring directly. This approach is more memory-efficient for millions of rows, as it only extracts the required portion without splitting the entire string into a list.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use split(',', 2) and take the second element
Why it's wrong here
split(',', 2) still builds a three-element list because the second argument is the maximum number of splits, not the number of pieces. The resulting list is ['John', 'Doe', '30,New York'], so the second element ('Doe') is correct only if the CSV has exactly two commas before the last name. This approach is less efficient than slicing because it allocates a list and copies all substrings, including the unwanted remainder.
- ✗
Use partition(',') and get the third element
Why it's wrong here
partition(',') returns a tuple with exactly three elements: the part before the first comma, the comma itself, and the entire rest of the string. The 'third element' is `'Doe,30,New York'`, which includes everything after the first comma, not just the last name. Even though it avoids a full split, it only finds the first comma, so it cannot isolate the field between the first and second commas.
- ✗
Use rsplit(',', 1) and take the first part
Why it's wrong here
rsplit(',', 1) splits from the right, performing exactly one split and returning a two-element list. Since the last comma precedes 'New York', the result is ['John,Doe,30', 'New York']; taking the first part gives `'John,Doe,30'`, which is the entire string except the final field, not the last name. The method correctly targets the last comma but fails because the desired last name is not adjacent to that comma.
- ✓
Use string slicing after finding the comma positions: start = row.find(',')+1; end = row.find(',', start); name = row[start:end]
Why this is correct
This technique directly extracts the substring between the first and second commas using index arithmetic. `row.find(',')` returns the index of the first comma, so adding 1 gives the character position immediately after it; then `row.find(',', start)` scans forward from that position to locate the second comma. Slicing `row[start:end]` copies only the needed characters and avoids creating a list of all fields, making it both memory-efficient and precise for this fixed-position CSV pattern.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 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.