Practice mode Python

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.