Text is a row of letters
After this: Read one character or a slice by position, and find the length.
On this page · 8 sections · 3 checks
A name typed into a form. A postcode. A message. Programs spend most of their time on text, and most of that work is in small pieces: the first letter of a name, the part of an email address before the @, how many characters a password has.
In "Lists" you kept many values in one row and read any one of them by its position. Text works the same way. A piece of text is a row too, and every letter in it has a position.
A row of characters
The word for one letter, digit, space or sign in a piece of text is a . A piece of text, stored as a row of characters, is called a . The text "hello" is a string of five characters.
Each character has a position, and as in Lists, the first position is 0:
| Position | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Character | h | e | l | l | o |
The word Lists used for a position was index. Both words mean the same thing here.
One character
To read one character, give its position after the name, as you did with a list, in square brackets; the Java spelling is in the note below. s[0]s.charAt(0) is the character at position 0, which is h. s[4]s.charAt(4) is the last one, o.
A piece of the row
Often you want more than one character: the first three, or everything after the first. A piece of a string, taken from one position up to another, is called a .
s[1:4]s.substring(1, 4) is the slice of "hello" from position 1 up to position 4. Work it out one position at a time:
- Position 1 is
e. Take it. - Position 2 is
l. Take it. - Position 3 is
l. Take it. - Position 4 is where the slice ends. Stop here, and do not take it.
The slice is ell. The rule: the start position is included, and the end position is not. A way to remember it: the end position is the first one you leave out. This is the same rule that range used in Repeating.
How long is it
len(s)s.length() gives the number of characters in the string: 5 for "hello". The last position is always one less than the length, because positions start at 0.
Watch it happen
The program below puts the three answers in boxes and prints them. Press Run it and step through.
- Run the programRun the program and step through it. Watch three boxes appear: one character, a slice of three characters, and the length as a number.
s = "hello"first = s[0]part = s[1:4]n = len(s)print(first)print(part)print(n)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
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Change "hello" to your own name and run it again. Which character is at position 0 now, and what is the length?
The ends
Two more things you will use all the time. Leaving out the end position means "to the end": s[2:]s.substring(2) is llo. And s[0:2]s.substring(0, 2) is the first two characters, he.
s = "hello"print(s[4])print(s[-1])print(s[0:2])print(s[2:])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
Check yourself
What does this print?
s = "garden"print(s[0])print(s[1:4])String s = "garden";System.out.println(s.charAt(0));System.out.println(s.substring(1, 4));The box s holds "hello". Which line gives llo?
Complete the missing line so that the program prints the length of s, which is 5. Put the length in the box n.
s = "hello"____________________ # ← put the length of s in the box nprint(n)String s = "hello";____________________ // ← put the length of s in the box nSystem.out.println(n);Honesty notes
- Say what a string is: text stored as a row of characters with positions that start at 0.
- Read one character by its position (s[0] in Python, s.charAt(0) in Java).
- Read a slice: from a start position up to, but not including, an end position (s[1:4] in Python, s.substring(1, 4) in Java).
- Find the length of a string (len(s) in Python, s.length() in Java).