AP Computer Science Principles · Unit 3 lecture · Part 3 of 14 · 6 min

3.4 StringsJoining and cutting text

Mr. Thompkins
Usernames, messages, file names: programs work with text all the time. The exam asks about two moves with text, joining and cutting, and one habit: count positions from 1.

A string is an ordered sequence of characters, written in quotes: "Hello", "B-12", "42". The quotes matter. "42" is a string of two characters, not the number 42, so you can't do arithmetic with it.

String concatenation joins two or more strings end to end to make a new string. A substring is part of an existing string. The reference sheet doesn't define procedures for these, so each exam question that uses them defines its own, for example concat(a, b), which returns b joined to the end of a, and substring(str, start, length), which returns length characters of str beginning at position start.

pineapple123456789positionsubstring("pineapple", 5, 5)is "apple"concat("pine", "apple") is "pineapple"nothing is added between, not even a space
Positions start at 1. Five characters from position 5 of "pineapple" are "apple".
?Wait, why does position start at 1? Python starts at 0.
Different languages make different choices. The AP exam's pseudocode counts from 1, for both string positions and list indexes. Whenever an exam question defines a string procedure, it will say where counting starts. Follow the question, not your favorite language.

Concatenation adds nothing between the strings: concat("sun", "day") is "sunday", with no space. If you want a space, you have to concatenate one: concat(concat("sun", " "), "day"). And concatenating number-looking strings just puts the characters side by side: concat("4", "2") is "42", not 6.

1first ← "Jordan"2last ← "Okafor"3user ← concat(substring(last, 1, 4), substring(first, 1, 1))14user ← concat(user, "26")25DISPLAY(user)
  1. line 3 "Okaf" joined to "J"
  2. line 4 The string "26" goes on the end
output › OkafJ26
Building a username from parts of two names.
expressionvalue
substring(last, 1, 4)"Okaf"
substring(first, 1, 1)"J"
user after line 3"OkafJ"
user after line 4"OkafJ26"
Evaluate the inside calls first, then the concat around them.
?Wait, what do nested calls like concat(substring(...), ...) mean?
Work from the inside out. Each inner call produces a value, and that value becomes an argument to the outer call. It's like the order of operations: the innermost parentheses go first.
Pause and think
Using substring(str, start, length) with position 1 as the first character, what is substring("planetarium", 1, 6)?
I thought about it. Show me
"planet": the first six characters, p-l-a-n-e-t.
Cut and join

concat(a, b) joins b to the end of a, and substring(str, start, length) returns length characters starting at position start, where position 1 is the first character. What is displayed?

day ← "saturday"
DISPLAY(concat(substring(day, 1, 3), substring(day, 6, 3)))
  1. Number the letters: s1 a2 t3 u4 r5 d6 a7 y8.
    Why?
    Writing the positions down prevents off-by-one mistakes.
  2. substring(day, 1, 3) is positions 1 to 3: "sat".
  3. substring(day, 6, 3) is positions 6, 7, 8: "day".
  4. concat("sat", "day") is "satday", with nothing in between.

Answer: satday

Stuck? Get a hint
  1. Write the position number under each letter of "saturday".
  2. Three characters starting at 6 means positions 6, 7 and 8.
  3. concat puts its first argument first.
Check yourself
Using the same definitions, what is concat(substring("notebook", 5, 4), substring("notebook", 1, 4))?
  1. A"notebook"
  2. B"booknote"
  3. C"ebooknot"
  4. D"book note"
Show the answer

B. Positions 5 to 8 of "notebook" are "book", and positions 1 to 4 are "note". concat puts the first argument first: "booknote". Choice C counts from 0, and D adds a space concat never adds.

  • A string is an ordered sequence of characters in quotes. "42" is text, not a number.
  • Concatenation joins strings end to end, adding nothing between them.
  • A substring is part of a string. Exam questions define their own string procedures.
  • Count string positions from 1, and evaluate nested calls from the inside out.