PCAP Strings Practice Question
What happens when you execute the following code? s = 'hello'; s[0] = 'H'
⚠ Common exam trap
The PCAP exam often tests the distinction between `TypeError` and `AttributeError` in the context of immutable types, trapping candidates who confuse 'cannot assign' with 'method not found'.
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
✓
TypeError is raised
In Python, strings 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`, not an `AttributeError` or `IndexError`, because the operation is a disallowed assignment on an immutable sequence type.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
AttributeError is raised
Why it's wrong here
An AttributeError indicates a problem with accessing or assigning to a nonexistent attribute, such as `s.foo = 1`. But `s[0] = 'H'` is item assignment, not attribute assignment. Python translates item assignment to a call to `__setitem__`, and since `str` objects don't implement that method for mutation, the failure is reported as a TypeError, not an AttributeError.
- ✓
TypeError is raised
Why this is correct
The operation `s[0] = 'H'` on a string invokes the sequence protocol's `__setitem__` method. Because `str` is an immutable type, it does not provide this mutator, so the interpreter raises `TypeError: 'str' object does not support item assignment`. This is Python's standard signal that the operation is unsupported for the given type, irrespective of the index value.
- ✗
String becomes 'Hello'
Why it's wrong here
Strings are immutable by design; once created, their characters cannot be changed in place. The assignment `s[0] = 'H'` does not alter the string; instead, it raises an exception before any modification can occur. To obtain 'Hello' from 'hello', you would need to create a new string with a method like `.capitalize()` or concatenation, not modify the original.
- ✗
IndexError is raised
Why it's wrong here
An IndexError would only be raised if the index were out of range for the sequence, such as `s[10]` on a five-character string. Here, `0` is a perfectly valid index, so the error cannot be about the index value. The assignment fails because the type is immutable, so Python raises TypeError first; the index is never even considered for bounds checking.
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.