Lesson 5.1
Length and one character by position
Part 5: Text, lists, stacks and queuesReading, about 4 min3 exercises in this lesson
Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.
Every character has a position
By the end of this you will be able to find how long a text value is, and read one character from it by its position.
So far a text value has been used whole: printed, joined to another, compared. A text value is really a row of characters, in order, and a program can reach inside it.
How long it is
word = "planet" print(len(word))
public static void main(String[] args) {
String word = "planet";
System.out.println(word.length());
}This prints 6. The length counts every character, spaces and punctuation too: "to do" has length 5.
One character by its position
Each character has a position, called its index. The first index is 0, not 1.
| index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| character | p | l | a | n | e | t |
word = "planet" print(word[2]) print(word[4])
public static void main(String[] args) {
String word = "planet";
System.out.println(word.charAt(2));
System.out.println(word.charAt(4));
}When it runs:
a e
In Python the index goes in square brackets after the name. In Java you ask for the character with charAt.
A common mistake: counting from 1. Index 2 holds the third character, a, not the second. The first character is at index 0.
One character on its own
word = "planet" ch = word[3] print(ch) print(len(ch))
public static void main(String[] args) {
String word = "planet";
char ch = word.charAt(3);
String asText = "" + ch;
System.out.println(asText);
System.out.println(asText.length());
}When it runs:
n 1
In Python, one character is a text value like any other, of length 1. In Java, charAt gives back a char: a separate type that holds exactly one character and is written with single quotes, so 'p' is a char and "p" is a String. Joining a char to an empty text value, written "" + ch, makes a String of it. That is what you need in Java when a question asks for text back.
The indexes stop one short of the length
Six characters use the indexes 0 to 5. There is no index 6, even though the length is 6. Ask for it and the program stops with an error:
word = "planet" print(word[6])
public static void main(String[] args) {
String word = "planet";
System.out.println(word.charAt(6));
}Python stops with IndexError: string index out of range. Java stops with a StringIndexOutOfBoundsException. Both mean the same thing: there is no character at that index.
This is true for any text with at least one character. The first index is 0, and the last index is one less than the length.
Now try it yourself on the questions below.
Exercise 5.1.1 · Trace itTrace: char at
Complete the trace table for this Python code. word[i] is the character at index i, counting from 0, and len(word) is the number of characters. [4]
Line 1: word = "code"Line 2: i = 1Line 3: a = word[i]Line 4: i = len(word) // 2Line 5: b = word[i]Line 6: print(a + b)
Fill in one row each time a line changes i, a or b, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | i | a | b | output |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
Exercise 5.1.1 · Trace itTrace: char at (Java)
Complete the trace table for this Java code. word.charAt(i) is the character at index i, counting from 0, and word.length() is the number of characters. [4]
Line 1: String word = "code";Line 2: int i = 1;Line 3: char a = word.charAt(i);Line 4: i = word.length() / 2;Line 5: char b = word.charAt(i);Line 6: System.out.println("" + a + b);
Fill in one row each time a line changes i, a or b, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Java shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | i | a | b | output |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
Exercise 5.1.25.1.2 · Write itFirst character
Write a function that returns the first character of a text value.
texthas at least one character. It contains only English letters, digits, spaces and punctuation.- Return the first character as a text value of length 1. In Java that means a
String, not achar.
Only the value your function returns is checked.
def first_char(text: str) -> str
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- first_char("hello") should return "h": the first of five letters (rule 2)
- first_char("A") should return "A": a single character (rule 1)
- first_char(" on") should return " ": a space is a character too, and here it is the first one
public static String firstChar(String text)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- firstChar("hello") should return "h": the first of five letters (rule 2)
- firstChar("A") should return "A": a single character (rule 1)
- firstChar(" on") should return " ": a space is a character too, and here it is the first one
Exercise 5.1.35.1.3 · Write itLast character
Write a function that returns the last character of a text value.
texthas at least one character. It contains only English letters, digits, spaces and punctuation.- Return the last character as a text value of length 1. In Java that means a
String, not achar.
Only the value your function returns is checked.
def last_char(text: str) -> str
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- last_char("hello") should return "o": the last of five letters (rule 2)
- last_char("A") should return "A": a single character (rule 1)
- last_char("go ") should return " ": a space is a character too, and here it is the last one
public static String lastChar(String text)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- lastChar("hello") should return "o": the last of five letters (rule 2)
- lastChar("A") should return "A": a single character (rule 1)
- lastChar("go ") should return " ": a space is a character too, and here it is the last one