Extracting Substrings Using String Slicing in Python
Which TWO of the following expressions yield the substring 'Py' from the string s = 'Python'?
Quick Answer
The correct answer is s[0:2], which extracts the substring 'Py' from the string 'Python' by slicing from index 0 up to, but not including, index 2. This works because Python string slicing uses a start:stop syntax where the stop index is exclusive, so indices 0 and 1—'P' and 'y'—are returned. On the Certified Associate Python Programmer PCAP exam, this tests your understanding of both positive and negative indexing, as well as the exclusive nature of the stop value. A common trap is forgetting that negative indices count from the end, so s[0:-4] also works because -4 corresponds to index 2, making the slice identical to s[0:2]. The exam frequently pairs these two equivalent forms to see if you recognize that both produce the same result. Memory tip: think of slicing like a fence—the start post is included, but the stop post is the first one you don't grab.
⚠ Common exam trap
Python Institute often tests the interaction between negative indexing and step values, trapping candidates who forget that a step of 2 skips characters or that negative indices count from the end, leading them to select options that return only one character or an incorrect substring.
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[0:-4]
S[0:-4] uses negative indexing to slice from index 0 up to (but not including) index -4, which corresponds to the character 'o' (the fifth character from the end). Since 'Python' has length 6, index -4 is the character at position 2 (0-based), so the slice returns characters at indices 0 and 1, which are 'P' and 'y', yielding 'Py'.
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[0:-4]
Why this is correct
Correct: from 0 to -4 (exclusive), which is indices 0 and 1.
- ✗
s[0:2:2]
Why it's wrong here
Yields 'P' because step 2 skips the second character.
- ✗
s[-6:-3]
Why it's wrong here
Yields 'Pyt' (indices -6, -5, -4).
- ✓
s[0:2]
Why this is correct
Correct: indices 0 and 1.
- ✗
s[0:1]
Why it's wrong here
Yields 'P' only.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 169-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 →
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. What is the result of the expression 'Hello'[1:3]?
easy- ✓ A.'el'
- B.'lo'
- C.'He'
- D.'ell'
Why A: In Python, string slicing uses the syntax `string[start:stop]`, where `start` is inclusive and `stop` is exclusive. For `'Hello'[1:3]`, the indices are: index 1 = 'e', index 2 = 'l', and index 3 is not included, so the slice returns 'el'. This is a fundamental string slicing behavior defined in Python's sequence protocol.
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.