What happens when the following Python code is run? s = “hello”; s = “H”
Correct answer B. A TypeError is raised
Explanation
Strings in Python are immutable, so item assignment such as s[0] = "H" raises TypeError because str does not support item assignment. To get "Hello" you must build a new string, for example "H" + s[1:] or s.capitalize(). IndexError would only occur for an index outside the string, which 0 is not.
Answer checked How we write and check questions
Discussion