PCAP Strings Practice Question
Which expression returns the last character of string s?
⚠ Common exam trap
The PCAP exam often tests the misconception that negative indexing starts at -0 or that len(s) is a valid index, leading candidates to pick s[len(s)] or s[-0] when they forget that indices are zero-based and negative indices count from -1 for the last element.
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[-1]
Python uses zero-based indexing for strings, where negative indices count from the end. s[-1] directly accesses the last character of the string, as -1 refers to the final element in the sequence.
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[-1]
Why this is correct
Python supports negative indices that count from the end of a sequence. An index of -1 specifically refers to the last element, so s[-1] evaluates to the final character of s, regardless of the string's length. Internally, Python converts negative indices by adding len(s), yielding len(s)-1, which is always the final valid position.
- ✗
s[len(s)]
Why it's wrong here
Using len(s) as an index is always one position past the final valid index, because valid indices for a sequence run from 0 to len(s)-1 inclusive. For any string s, s[len(s)] raises an IndexError: string index out of range, since the length itself is not a valid position. Even an empty string fails because its length is 0, and index 0 is already out of range, so this expression can never return the last character.
- ✗
s[-0]
Why it's wrong here
The expression -0 evaluates to the integer 0 in Python, because unary negation of zero yields zero, not a negative number. Therefore s[-0] is exactly equivalent to s[0] and selects the first character, not the last. This is a subtle mistake: a programmer may intend a negative offset but accidentally write -0, which carries no negative sign information.
- ✗
s[0]
Why it's wrong here
String indexing is zero-based, meaning the first character is stored at position 0 and the last character is at position len(s)-1. Consequently, s[0] returns the first character of the string, not the last one. For strings of length greater than one, clearly this does not satisfy the requirement, and even for a one-character string it only coincidentally equals the last character.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 421 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.