Skip to content
ExamSpine

Lesson 5.2

Substring by position

Part 5: Text, lists, stacks and queuesReading, about 5 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.

Taking part of a text value

By the end of this you will be able to take a run of characters out of a text value by saying where it starts and where it stops.

One index gives one character. Two indexes give a piece of the text, called a substring.

Start here, stop before there

word = "planet"
print(word[2:5])
public static void main(String[] args) {
    String word = "planet";
    System.out.println(word.substring(2, 5));
}

This prints ane. The first number is the index where the piece starts. The second is the index where it stops, and the character at that index is not included.

index012345
characterplanet

So 2 and 5 give the characters at indexes 2, 3 and 4. A quick check: the number of characters you get is the second index minus the first. 5 minus 2 is 3, and ane has 3 characters.

A common mistake: expecting the character at the second index to be included. The piece stops before the t at index 5.

Up to the end

To take everything from an index to the end, stop at the length. The length is one past the last index, which is exactly where "stop before" needs to be.

word = "planet"
print(word[3:len(word)])
public static void main(String[] args) {
    String word = "planet";
    System.out.println(
        word.substring(3, word.length()));
}

This prints net.

Both languages have a short way to write this. In Python, leave out the second number: word[3:]. In Java, give substring one number: word.substring(3).

Past the end

word = "planet"
print(word[3:9])
public static void main(String[] args) {
    String word = "planet";
    System.out.println(word.substring(3, 9));
}

Here the two languages differ. Python prints net: a piece that runs past the end stops at the end, and there is no error. Java stops with a StringIndexOutOfBoundsException. So in Python a second number that is too big can go unnoticed. Check it against the length.

The text itself does not change

Taking a piece makes a new text value. The original is still whole.

word = "planet"
piece = word[1:4]
print(piece)
print(word)
public static void main(String[] args) {
    String word = "planet";
    String piece = word.substring(1, 4);
    System.out.println(piece);
    System.out.println(word);
}

When it runs:

lan
planet

To use the piece again, keep it in a variable, as this program does with piece.

Now try it yourself on the questions below.

Exercise 5.2.15.2.1 · Write itFirst N characters

Write a function that returns the first n characters of a text value.

  1. text has at least one character. It contains only English letters, digits, spaces and punctuation.
  2. n is at least 1, and at most the number of characters in text.

Only the value your function returns is checked.

def first_n_chars(text: str, n: int) -> str

Examples

Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.

  • first_n_chars("hello", 3) should return "hel": the first 3 of 5 characters (rule 2)
  • first_n_chars("abcdef", 1) should return "a": n is 1, the smallest it can be (rule 2)
  • first_n_chars("xyz", 3) should return "xyz": n is the whole length, so the whole text comes back (rule 2)

public static String firstNChars(String text, int n)

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.

  • firstNChars("hello", 3) should return "hel": the first 3 of 5 characters (rule 2)
  • firstNChars("abcdef", 1) should return "a": n is 1, the smallest it can be (rule 2)
  • firstNChars("xyz", 3) should return "xyz": n is the whole length, so the whole text comes back (rule 2)
solution.pySolution.java

Exercise 5.2.25.2.2 · Write itRemove first and last

Write a function that returns a text value without its first character and without its last character.

  1. text has at least 3 characters. It contains only English letters, digits, spaces and punctuation.
  2. Every other character is returned, in the same order.

Only the value your function returns is checked.

def remove_ends(text: str) -> str

Examples

Your function is tested on 9 cases: the 3 examples below, and 6 hidden ones that check the rules above.

  • remove_ends("hello") should return "ell": h and o are removed, and the three letters between them stay (rule 2)
  • remove_ends("abc") should return "b": three characters, the fewest allowed: one is left (rule 1)
  • remove_ends("(done)") should return "done": the first and last characters here are brackets (rule 1)

public static String removeEnds(String text)

Checking whether Java answers can run here.

Examples

Your function is tested on 9 cases: the 3 examples below, and 6 hidden ones that check the rules above.

  • removeEnds("hello") should return "ell": h and o are removed, and the three letters between them stay (rule 2)
  • removeEnds("abc") should return "b": three characters, the fewest allowed: one is left (rule 1)
  • removeEnds("(done)") should return "done": the first and last characters here are brackets (rule 1)
solution.pySolution.java

Exercise 5.2.35.2.3 · Write itSwap halves

Write a function that swaps the two halves of a text value.

  1. text has an even number of characters, and at least 2. It contains only English letters, digits, spaces and punctuation.
  2. The two halves have the same number of characters. The first half is the one at the start of text.
  3. Return the second half followed by the first half.

Only the value your function returns is checked.

def swap_halves(text: str) -> str

Examples

Your function is tested on 10 cases: the 3 examples below, and 7 hidden ones that check the rules above.

  • swap_halves("abcd") should return "cdab": ab and cd change places (rule 3)
  • swap_halves("ab") should return "ba": two characters, the fewest allowed: each half is one character (rule 1)
  • swap_halves("a tree") should return "reea t": a space is a character, and moves with its half (rule 2)

public static String swapHalves(String text)

Checking whether Java answers can run here.

Examples

Your function is tested on 10 cases: the 3 examples below, and 7 hidden ones that check the rules above.

  • swapHalves("abcd") should return "cdab": ab and cd change places (rule 3)
  • swapHalves("ab") should return "ba": two characters, the fewest allowed: each half is one character (rule 1)
  • swapHalves("a tree") should return "reea t": a space is a character, and moves with its half (rule 2)
solution.pySolution.java