Text cannot change
After this: Say why s[0] cannot be given a new value, and build a new string instead.
On this page · 5 sections · 3 checks
The box s holds "hello", and you want "Hello". With a list you would write marks[0] = 50marks.set(0, 50) to put a new value at position 0. So try the same with the string: put an H at position 0.
s = "hello"s[0] = "H"print(s)TypeError: 'str' object does not support item assignment
String s = "hello";s.charAt(0) = 'H';System.out.println(s);error: unexpected type required: variable found: value
Python stops the program on line 2 with the error shown. Nothing is printed.Java refuses to compile the program: line 2 is reported as an error before anything runs. The same thing happens for any position and any character.
A string is fixed
A list can be changed in place: a value at one position can be swapped for another. A string cannot. Once a string exists, no character in it can be replaced, added or removed. You can read any position you like, as the first lesson showed, but you cannot write to one.
This fits what you saw in the last lesson. s.upper()s.toUpperCase() did not turn the box s into capitals. It gave back a new string in capitals and left s as it was. Every action on text works that way: a new string comes back, and the old one is untouched.
Build a new string instead
So how do you get "Hello"? You build it. Take the pieces you want from the old string, join them with the new piece, and keep the result in a box.
- The new first character is
"H". - The rest of the old string is the slice from position 1 to the end:
ello. - Join them:
"H" + "ello"is"Hello". Put it in a box.
The trace below does this, and then one more thing: on line 46 it gives the box s itself a new string, s + "!". Watch the box. The old hello is not changed; it is replaced by a new string, hello!, in the same way that age = age + 1 replaces a number.
s = "hello"t = "H" + s[1:]print(t)s = s + "!"print(s)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Change the "H" to "J" and run again. Then make the last line print s in capitals without changing s.
Here is the same idea with an action in the middle: the first character in capitals, joined to the rest.
name = "ana"fixed = name[0].upper() + name[1:]print(name)print(fixed)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Keep the result
The most common slip with text is to use an action and throw its answer away. The second line below does the work and loses it; the fourth line keeps it by putting the answer back in the box.
s = "hi"s.upper()print(s)s = s.upper()print(s)hi HI
String s = "hi";s.toUpperCase();System.out.println(s);s = s.toUpperCase();System.out.println(s);hi HI
The rule: an action on text changes nothing by itself. If you want the new string, put it in a box.
Check yourself
The box s holds "hello". Why does s[0] = "H"s.charAt(0) = 'H' fail?
What does this print?
s = "cat"t = s[0:2] + "r"print(s)print(t)String s = "cat";String t = s.substring(0, 2) + "r";System.out.println(s);System.out.println(t);The box s holds "hello". Complete the missing line so that t holds Hello: a capital H followed by the rest of s.
s = "hello"____________________ # ← t is s with a capital H at the frontprint(t)String s = "hello";____________________ // ← t is s with a capital H at the frontSystem.out.println(t);Honesty notes
- Say why giving s[0] a new value fails: a string cannot be changed in place.
- Build a new string from pieces of an old one, such as "H" + s[1:], and keep it in a box.
- Say that every action on text gives back a new string and leaves the old one as it was.
- Keep the result of an action: s = s.upper(), not s.upper() on its own.