String Immutability — Understanding TypeError on Assignment
A developer writes: s = 'abc'; s[0] = 'x'. What happens?
Quick Answer
The correct answer is that the code raises a TypeError: 'str' object does not support item assignment. This happens because strings in Python are immutable, meaning once a string object is created, its individual characters cannot be changed in place. When you attempt to assign a new value to an index like `s[0] = 'x'`, the interpreter immediately rejects the operation at runtime, enforcing the immutability contract of the `str` type. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of Python’s core data type properties and the distinction between mutable and immutable objects. A common trap is confusing strings with lists, which do support item assignment; many beginners expect strings to behave the same way. To remember this, think of a string as a sealed envelope—you cannot erase and rewrite a single letter inside; you must create a whole new envelope.
⚠ Common exam trap
Python Institute often tests the immutability of strings by presenting an assignment to an index, tricking candidates who confuse strings with mutable sequences like lists.
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: 'str' object does not support item assignment
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] = 'x'`) raises a `TypeError: 'str' object does not support item assignment`. This is a fundamental property of the `str` type in Python, enforced at the interpreter level.
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 becomes 'xbc'
Why it's wrong here
Strings do not support item assignment.
- ✓
TypeError: 'str' object does not support item assignment
Why this is correct
This is the exact error raised.
- ✗
ValueError: string index out of range
Why it's wrong here
Index 0 is valid, so no IndexError; the error is TypeError about assignment.
- ✗
s becomes 'abc' and no error
Why it's wrong here
An error is raised; the string remains unchanged.
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 →
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. A developer tries to modify a string: s = 'hello'; s[0] = 'H'. What happens when this code runs?
medium- A.It changes the string to 'Hello'
- ✓ B.It raises a TypeError: 'str' object does not support item assignment
- C.It creates a new string 'Hello' and assigns it to s
- D.It raises an IndexError because index 0 is out of range
Why B: 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.
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.