Question Detail

COS 102 Advanced Text Processing Objective Question

Question

Why does `name[0] = "J"` result in a `TypeError` when `name = "Python"`?
Options
A Because strings can only be accessed using negative indices.
B Because individual characters in a string cannot be reassigned; strings are immutable.
Correct Answer
C Because 'J' is not a valid character for a string.
D Because you must use a string method to modify the first character.
Correct Answer

Option B is the correct answer.

Detailed Explanation

The document explicitly shows this example: name[0] = "J" # CRASH! TypeError!. It explains that 'Immutable means a string cannot be altered once it's created in memory,' so you cannot change individual characters directly by assignment.

Hint

Refer back to the 'Strings are Immutable!' concept.

Question Info