In Python, a student runs: s = 'Hello'; s[0] = 'h'. What happens?
As becomes 'hello' — the first character is lowercased in place
Bs becomes 'hHello' — the character is inserted at position 0
CA TypeError or similar error is raised — strings are immutable
DNothing happens — the assignment is silently ignored
Strings are immutable in Python, meaning you cannot change individual characters after the string is created. Attempting s[0] = 'h' raises a TypeError: 'str' object does not support item assignment. To achieve the effect, you must create a new string: s = 'h' + s[1:]. This immutability is a deliberate design choice — it makes strings safe to pass around since no function can silently alter a string you hand it.
Question 2 Multiple Choice
The string s = 'Python' has length 6. What is the value of s[5]?
A'o' — the fifth letter of Python
BAn IndexError — index 5 is out of range for a 6-character string
C'n' — the last character, at index 5
D'P' — index 5 counts 5 characters from the end
String indexing starts at 0, so a string of length 6 has valid indices 0 through 5. s[0]='P', s[1]='y', s[2]='t', s[3]='h', s[4]='o', s[5]='n'. The last valid index is always length minus 1. The off-by-one relationship between length (6) and last index (5) is a consistent source of bugs — if you try s[6], you get an IndexError. Think of the index as an offset from the start: the first character is 0 steps in, the last is 5 steps in.
Question 3 True / False
In Python, every string operation that appears to modify a string actually creates a new string; the original string object is unchanged.
TTrue
FFalse
Answer: True
String immutability means operations like concatenation, slicing, and case conversion always produce new string objects. If you write s = s.upper(), the variable s is reassigned to point to a new uppercase string — the original string object still exists unchanged in memory (until garbage collected). This matters when you pass strings to functions: a function cannot alter the original string, only return a new one. Immutability makes strings reliable across multiple references to the same object.
Question 4 True / False
In Python, the string '42' and the integer 42 can be used interchangeably in arithmetic expressions.
TTrue
FFalse
Answer: False
The string '42' and the integer 42 are completely different types. Attempting '42' + 3 raises a TypeError in Python because you cannot add a string and an integer directly. In some languages (like JavaScript), this produces '423' through implicit type coercion — a different problem entirely. Explicit conversion is always required: int('42') converts the string to 42, or str(42) converts the integer to '42'. This distinction is critical whenever reading numeric data from files, user input, or APIs, which always arrive as strings.
Question 5 Short Answer
Explain why string immutability is a useful design choice, not just a limitation. What problems would arise if strings could be modified in place?
Think about your answer, then reveal below.
Model answer: Immutability makes strings safe to share across multiple parts of a program. If strings were mutable, passing a string to a function would risk that function silently altering it — a hard-to-debug side effect. With immutability, you can safely pass a string anywhere knowing it cannot be changed. Immutability also enables optimizations like string interning (sharing a single copy of equal strings) and makes strings usable as dictionary keys (which require objects that don't change).
Languages with mutable strings (like C's character arrays) require defensive copying whenever a string is passed to untrusted code. Python's immutability eliminates this class of bug entirely: every operation produces a new string, so the original is always intact. The cost is that building strings in a loop through concatenation is inefficient (each + creates a new object), which is why Python provides ''.join() for that pattern. Understanding immutability also prepares you for the same concept appearing in other data types, like tuples versus lists.