BugLab015 / 020up to 100
stringsimmutabilitytypeseasy

Read-Only

The function toggle_case_first(s) should return a new string where the first character's case is toggled — uppercase becomes lowercase, lowercase becomes uppercase. The rest of the string stays the same.

The function crashes immediately with a TypeError. Why can't Python do what the code is asking?

hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def toggle_case_first(s):
"""
Returns the string with the first character's case toggled.
Uppercase becomes lowercase; lowercase becomes uppercase.
The rest of the string is unchanged.
Example: toggle_case_first("Hello") -> "hello"
"""
if s[0].isupper():
s[0] = s[0].lower()
else:
s[0] = s[0].upper()
return s
>>> toggle_case_first('Hello')