String Immutability
In Python, strings are immutable: once created, the characters they contain cannot be changed in place. Any "modification" you do actually creates a new string. This matters for correctness (no accidental in-place edits) and for performance (how you build larger strings).
No In-Place Edits
You can read characters by index, but you cannot assign to them.
123456s = "hello" t = "H" + s[1:] # Creates a new string: "Hello" print(t) s[0] = "H" # TypeError: strings don't support item assignment
Most string methods return a new string and leave the original unchanged.
123456name = " Alice " name.strip() # returns "Alice" but doesn't change 'name' print(name) # " Alice " name = name.strip() # assign the new value print(name) # "Alice"
Chaining is fine, but remember you're getting a new object each step.
1234msg = " hello\n" clean = msg.strip().upper() print(msg) # original remains " hello\n" print(clean) # "HELLO"
"Modifying" by creating a new string
Use slicing, replace
, or concatenation to produce a new value.
1234567s = "data" s = s.replace("t", "T") # "daTa" print(t) s = s[:1] + "A" + s[2:] # "dAta" print(t)
Efficient building
Repeated +
in large loops can be slow (many intermediate strings). A common pattern is to collect pieces and join once:
12parts = ["ID:", " ", "123", "\n", "OK"] result = "".join(parts) # "ID: 123\nOK"
You'll learn more joining/formatting patterns in the next chapter.
1. Which line attempts to modify a string in place and will raise an error?
2. What will the code output?
3. You need to assemble a long string from many small pieces. What's recommended?
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain why strings are immutable in Python?
What happens if I try to modify a string in place?
How can I efficiently build large strings in Python?
Awesome!
Completion rate improved to 5
String Immutability
Sveip for å vise menyen
In Python, strings are immutable: once created, the characters they contain cannot be changed in place. Any "modification" you do actually creates a new string. This matters for correctness (no accidental in-place edits) and for performance (how you build larger strings).
No In-Place Edits
You can read characters by index, but you cannot assign to them.
123456s = "hello" t = "H" + s[1:] # Creates a new string: "Hello" print(t) s[0] = "H" # TypeError: strings don't support item assignment
Most string methods return a new string and leave the original unchanged.
123456name = " Alice " name.strip() # returns "Alice" but doesn't change 'name' print(name) # " Alice " name = name.strip() # assign the new value print(name) # "Alice"
Chaining is fine, but remember you're getting a new object each step.
1234msg = " hello\n" clean = msg.strip().upper() print(msg) # original remains " hello\n" print(clean) # "HELLO"
"Modifying" by creating a new string
Use slicing, replace
, or concatenation to produce a new value.
1234567s = "data" s = s.replace("t", "T") # "daTa" print(t) s = s[:1] + "A" + s[2:] # "dAta" print(t)
Efficient building
Repeated +
in large loops can be slow (many intermediate strings). A common pattern is to collect pieces and join once:
12parts = ["ID:", " ", "123", "\n", "OK"] result = "".join(parts) # "ID: 123\nOK"
You'll learn more joining/formatting patterns in the next chapter.
1. Which line attempts to modify a string in place and will raise an error?
2. What will the code output?
3. You need to assemble a long string from many small pieces. What's recommended?
Takk for tilbakemeldingene dine!