Challenge - 5 Problems
String Immutability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of string modification attempt
What is the output of this Python code?
Python
s = "hello" s[0] = 'H' print(s)
Attempts:
2 left
๐ก Hint
Remember that strings cannot be changed by assigning to an index.
โ Incorrect
Strings in Python are immutable, so trying to assign a new character to an index causes a TypeError.
โ Predict Output
intermediate2:00remaining
Result of string concatenation
What is the output of this code?
Python
s = "cat" s = s + "s" print(s)
Attempts:
2 left
๐ก Hint
Concatenation creates a new string.
โ Incorrect
Concatenating strings creates a new string; the original string is not changed but reassigned.
โ Predict Output
advanced2:00remaining
Output after string replace method
What is printed by this code?
Python
s = "banana" s.replace('a', 'o') print(s)
Attempts:
2 left
๐ก Hint
Does the replace method change the original string?
โ Incorrect
The replace method returns a new string but does not change the original string because strings are immutable.
โ Predict Output
advanced2:00remaining
Value of string after slicing and concatenation
What is the output of this code?
Python
s = "python" s = 'J' + s[1:] print(s)
Attempts:
2 left
๐ก Hint
Slicing creates a new string; concatenation creates a new string.
โ Incorrect
The code creates a new string by concatenating 'J' with the slice s[1:], resulting in 'Jython'.
โ Predict Output
expert2:00remaining
Final string value after chained operations
What is the output of this code?
Python
s = "immutable" s1 = s.upper() s2 = s1.replace('A', '@') s3 = s2.lower() print(s3)
Attempts:
2 left
๐ก Hint
Check the order of operations and what each method returns.
โ Incorrect
The final string s3 is the lowercase version of s2, which replaced 'A' with '@' in uppercase s1. 'IMMUTABLE' contains 'A', so s2 is 'IMMUT@BLE', and s3 is 'immut@ble'.