PCAP Strings Practice Question
Which of the following demonstrates that strings are immutable?
⚠ Common exam trap
Python Institute often tests the misconception that methods like upper(), replace(), or the += operator modify the original string in place, when in fact they always return a new string object, and the trap is that candidates confuse variable rebinding with in-place mutation.
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] = 'J' results in a TypeError
Attempting to assign a new character to an index of a string (e.g., s[0] = 'J') raises a TypeError, which directly demonstrates that strings are immutable in Python. Immutability means the object's value cannot be changed after creation; any operation that appears to modify a string actually creates a new string object.
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.upper() changes s in place
Why it's wrong here
Calling s.upper() does not change s in place. Since str objects are immutable, upper() cannot modify the existing string; instead, it creates and returns a brand-new str object containing the uppercased characters, leaving the original object untouched. To see the effect, you would have to bind the returned value to a name, which is rebinding, not mutation.
- ✓
s[0] = 'J' results in a TypeError
Why this is correct
The statement s[0] = 'J' raises a TypeError because assignment to an indexed position attempts to modify the contents of an existing str object, and immutable objects do not support item assignment. The interpreter explicitly forbids this operation, which is the most direct and unambiguous demonstration of string immutability.
- ✗
s += '!' modifies s
Why it's wrong here
The augmented assignment s += '!' does not modify the original string object. Concatenation is not an in-place operation for immutable types; it allocates a new str object whose contents are the old string plus the exclamation mark, then rebinds the name s to that new object. Any other references to the original string remain unchanged, proving the original object was never mutated.
- ✗
s.replace('a','b') modifies s
Why it's wrong here
The method s.replace('a', 'b') does not modify s. Because strings are immutable, replace() must construct and return a new str object with the specified replacements applied, while the original string remains completely unchanged. If the result is not assigned to a variable, the original s is left as it was, so no in-place modification occurs.
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 →
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.