PCAP Strings Practice Question
A developer tries to modify a string: s = 'hello'; s[0] = 'H'. What happens when this code runs?
⚠ Common exam trap
The PCAP exam often tests the immutability of strings by presenting an assignment to an index, tricking candidates who confuse strings with mutable sequences like lists into thinking the string will be modified in place.
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
✓
It raises a TypeError: 'str' object does not support item assignment
Strings in Python are immutable, meaning their contents cannot be changed after creation. Attempting to assign a new character to an index position (e.g., s[0] = 'H') raises a TypeError: 'str' object does not support item assignment. To modify a string, you must create a new string using slicing or concatenation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
It changes the string to 'Hello'
Why it's wrong here
This outcome assumes strings behave like mutable arrays, but the assignment s[0] = 'H' does not mutate the existing object. Python's str type lacks the __setitem__ method, so the interp receives a request to change the character at index 0 and immediately aborts with a TypeError before any modification occurs. Therefore, the original 'hello' remains untouched and no 'Hello' is ever produced.
- ✓
It raises a TypeError: 'str' object does not support item assignment
Why this is correct
Strings in Python are immutable sequences; the assignment `s[0] = 'H'' attempts to mutate the object at index 0, which violates the immutable contract of the `str` type. The interpreter raises a `TypeError` specifically because `str` objects lack a `__setitem__` method, preventing item assignment. This directly satisfies the constraint that strings cannot be modified in-place in Python.
- ✗
It creates a new string 'Hello' and assigns it to s
Why it's wrong here
This option confuses rebinding the name s to a new string with the in-place mutation attempted by index assignment. The expression s[0] = 'H' is a store-subscript operation that requires the target object to support item assignment; str does not, so Python raises a TypeError before any new string object is constructed or any name rebinding happens. Reassignment via s = 'Hello' would create a new string, but that is not what the code executes.
- ✗
It raises an IndexError because index 0 is out of range
Why it's wrong here
IndexError is raised only when an index is out of the valid range for a subscriptable sequence, but index 0 is perfectly valid in the string 'hello'—it refers to the character 'h'. The actual failure occurs because the runtime detects that the target is an immutable str and invokes its missing __setitem__, which triggers a TypeError. Thus the error type is not about bounding, but about the unsupported assignment operation on an immutable type.
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.