A string is an ordered sequence of characters, such as "big data", where letters, digits, spaces and punctuation all count. Concatenation joins two strings end to end, adding nothing between them, and a substring is a run of characters taken from an existing string. In AP® Computer Science Principles, string procedures come with a definition in the question, so you trace them by following that definition exactly, including where it starts counting positions.
Quick check
After name ← "Ravi", what value does name hold?Show answerHide answer
It holds the string "Ravi". The quotation marks show where the string starts and ends; they are not part of it.
How many characters are in "go home"?Show answerHide answer
Seven: g, o, the space, h, o, m and e. A space is a character.
Know these already? Jump to the practice questions
What you'll be able to do
- Find the length of a string and the position of each character.
- Predict the result of joining strings with a concatenation procedure.
- Use the definition a question gives to extract a substring.
- Determine what code displays when it builds a new string from parts of others.
Key terms
String
An ordered sequence of characters, written between quotation marks.
- In plain English
- Text, kept in a fixed order.
- Don't write
- "A string can only hold letters."
Concatenation
Joining two strings end to end to make a new string.
- In plain English
- Gluing one piece of text straight onto the end of another.
- Don't write
- "Concatenating "4" and "5" gives 9."
Substring
A part of an existing string made of consecutive characters.
- In plain English
- A slice cut from a longer string.
- Don't write
- "A substring can skip characters in the middle."
What makes a string different from a number?
A string's characters sit in a fixed order, and each has a position. Here, position 1 is the first character.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Character | b | i | g | (space) | d | a | t | a |
"big data" has 8 characters, because the space counts. The empty string "" has 0.
Digits inside quotation marks are characters, not a number. "42" is a string of two characters, so joining it to "1" gives "421", not 43. DISPLAY shows a string's characters without the quotation marks.
Learn this: the exam reference sheet does not define string procedures. When a question uses one, it tells you what it does, and that definition is the rule you follow.
Check yourself
Show answerHide answer
There are 8 characters, and position 4 holds the space.
How does concatenation join strings?
Here, concat(str1, str2) returns a new string: str1 followed immediately by str2.
- Nothing is added between them:
concat("ice", "cream")is "icecream". - Order matters:
concat("cream", "ice")is "creamice". - To get a space, join one in:
concat(concat("a", " "), "b")is "a b".
With nested calls, work from the inside out, exactly as with parentheses in arithmetic. The length of the result is always the sum of the two lengths, which makes a quick check.
Key idea
Concatenation adds nothing and changes nothing: the result is the first string's characters followed by the second's.
Check yourself
Show answerHide answer
"a b". The inner call gives "a " (with a space), and the outer call adds "b".
How do you read a substring definition?
Every substring procedure answers two questions, and the definition in the question tells you both:
- Where does counting start? Usually position 1, but read it.
- Is the second number a length or a last position?
Take "planet" (p1 l2 a3 n4 e5 t6) and the numbers 2 and 3.
| Definition | Call | Result |
|---|---|---|
substring(str, start, length): length characters from position start | substring("planet", 2, 3) | "lan" |
slice(str, first, last): positions first to last, both included | slice("planet", 2, 3) | "la" |
Same numbers, different results, so the definition decides. Either way, the original string is not changed: the procedure returns a new string, and a variable changes only if you assign the result to it.
Check yourself
Show answerHide answer
"book": the 4 characters at positions 5, 6, 7 and 8.
Worked example 1: Trace concatenation
Question: What is displayed when this code segment runs? concat(str1, str2) returns str1 followed immediately by str2.
a ← "cup"
b ← "cake"
c ← concat(b, a)
c ← concat(c, "s")
DISPLAY(c)
What tells you the method
Each line stores one result, so trace the variables line by line.
Step 1: Trace each assignment.
Line a b c 1–2 "cup" "cake" 3 "cup" "cake" "cakecup" 4 "cup" "cake" "cakecups" WhyLine 3 puts
bfirst because it is the first argument; line 4 uses the current value ofc.Step 2: Write what is displayed.
"cakecups", without quotation marks.
WhyDISPLAYshows the characters of the string.
Check it
Lengths 4 + 3 + 1 = 8, and "cakecups" has 8 characters.
Answer
cakecups
Worked example 2: Design a date reformatter
Question: date holds a date written as year-month-day, such as "2027-03-15". Design a code segment that displays it as day.month.year. Use concat (as above) and substring(str, start, length), which returns length characters of str from position start, where the first character is at position 1. What is displayed for "2027-03-15"?
What tells you the method
Rearranging parts of a string is substring then concatenation.
Step 1: State the input, output and plan.
Number the positions: year 1–4, month 6–7, day 9–10. Cut out each part, then join them in the new order with "." between.
WhyFixed positions mean each part has a fixed start and length.
Step 2: Write the code.
day ← substring(date, 9, 2) month ← substring(date, 6, 2) year ← substring(date, 1, 4) out ← concat(day, ".") out ← concat(out, month) out ← concat(out, ".") out ← concat(out, year) DISPLAY(out)WhyBuilding
outone join at a time keeps each step easy to trace.Step 3: Trace it on "2027-03-15".
dayis "15",monthis "03" andyearis "2027", sooutbecomes "15.03.2027".WhyA design is tested by tracing it on a real input.
Check it
Trace "2026-11-30": the parts are "30", "11" and "2026", giving "30.11.2026".
Answer
15.03.2027
Worked example 3: Follow an unfamiliar definition
Question: In this question, slice(str, first, last) returns the characters of str from position first to position last, including both, where the first character is at position 1. What does slice("lighthouse", 3, 6) return?
- A) "htho"
- B) "ghthou"
- C) "ghth"
- D) "ght"
What tells you the method
A new procedure with its own definition: number the positions, then apply the definition word for word.
Step 1: Number the positions.
l1 i2 g3 h4 t5 h6 o7 u8 s9 e10.
WhyThe definition counts from 1, so the numbering must too.
Step 2: Apply the definition.
Positions 3 to 6, both included, hold g, h, t and h.
WhyHere the second number is a last position, not a length.
Check it
From 3 to 6 inclusive is 6 − 3 + 1 = 4 characters, and "ghth" has 4.
Answer
C
Your turn
Question: w holds "keyboard". Using concat and substring(str, start, length) as defined in Worked example 2, what does DISPLAY(concat(substring(w, 4, 5), substring(w, 1, 3))) display?
Step 1: Number the positions and do the first substring.
k1 e2 y3 b4 o5 a6 r7 d8, so
substring(w, 4, 5)is the 5 characters from position 4: "board".Step 2: Your go: do the second substring, then join the two.
Show the full solutionHide the solution
Step 2. substring(w, 1, 3) is "key". concat("board", "key") puts "board" first.
Answer: boardkey
Traps
Trap
concat puts a space between the two strings.
It seems right because written words have spaces between them. But concatenation adds nothing, so concat("ice", "cream") is "icecream". A space appears only if you join one in.
What does concat("hot", "dog") return?
"hotdog", with no space.
Trap
Positions always start at 0.
It seems right because many programming languages number characters from 0. But the question's definition decides. When it says the first character is at position 1, substring("table", 2, 3) is "abl".
Counting from position 1, what is substring("table", 2, 3) with a (start, length) definition?
"abl": the 3 characters starting at position 2.
Trap
The second number in a substring call is where the substring ends.
It seems right because two numbers look like a start and an end. But under a (start, length) definition the second number counts characters, so substring("table", 2, 3) keeps 3 characters, not positions 2 to 3.
Under slice(str, first, last) with both ends included, what is slice("table", 2, 3)?
"ab": positions 2 and 3 only.
What's examined
On the AP® Computer Science Principles exam, strings belong to Big Idea 3: Algorithms and Programming. Multiple-choice questions define a string procedure and ask what a code segment displays or which call gives a result. String procedures are Learn this, while DISPLAY and ← are on the exam reference sheet.
Practice questions
The definitions below are for questions 1 to 7. concat(str1, str2) returns str1 followed immediately by str2. substring(str, start, length) returns length characters of str beginning at position start, where the first character is at position 1. len(str) returns the number of characters in str.
- Recall What does
concat("8", "4")return?
- A) 12
- B) "84"
- C) "48"
- D) "8 4"
Show answer
Answer: B. The two strings are joined end to end, in order.
Why the others are wrong:
- A adds the digits as numbers, but they are strings.
- C reverses the order of the arguments.
- D inserts a space that
concatnever adds.
- Recall What value does
len("red, blue")return?
Show answer
Answer: 9
Marking points:
- r, e, d, the comma, the space, b, l, u, e: the comma and space both count.
- Apply What is displayed by
DISPLAY(substring("rainbow", 3, 4))?
Show answer
Answer: inbo
Marking points:
- r1 a2 i3 n4 b5 o6 w7: 4 characters from position 3 are i, n, b and o.
- Exam-style
wordholds "airport". Which call returns "port"?
- A)
substring(word, 3, 4) - B)
substring(word, 4, 3) - C)
substring(word, 5, 3) - D)
substring(word, 4, 4)
Show answer
Answer: D. a1 i2 r3 p4 o5 r6 t7: "port" starts at position 4 and has 4 characters.
Why the others are wrong:
- A counts from 0, so it starts one too early and gives "rpor".
- B takes 7 − 4 = 3 as the length and gives "por".
- C starts one too late and gives "ort".
- Exam-style What is displayed when this code segment runs?
word ← "stone"
n ← len(word)
word ← concat(substring(word, 2, n - 1), substring(word, 1, 1))
DISPLAY(word)
word ← concat(substring(word, 2, n - 1), substring(word, 1, 1))
DISPLAY(word)
Show answer
Answer: tones onest
Marking points:
nis 5. The first pass joins "tone" and "s": "tones".- The second pass works on "tones": "ones" and "t" give "onest".
- The two
DISPLAYcalls show their values on one line, separated by a space.
- Exam-style This code segment should display the first three letters of a first name followed by the last two digits of a year, here "Mar09". Give the number of the line that contains the error.
1: first ← "Marisol"
2: year ← "2009"
3: part1 ← substring(first, 1, 3)
4: part2 ← substring(year, 2, 2)
5: user ← concat(part1, part2)
6: DISPLAY(user)
Show answer
Answer: Line 4.
Marking points:
- Positions 2 and 3 of "2009" are "00", so the code displays "Mar00".
- The last two digits are at positions 3 and 4:
substring(year, 3, 2)gives "09".
- Apply
aholds "hot" andbholds "dog". What value doeslen(concat(a, concat(" ", b)))return?
Show answer
Answer: 7
Marking points:
- The inner call gives " dog" (4 characters), and joining "hot" gives "hot dog".
- 3 + 1 + 3 = 7.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- Every character counts toward a string's length, spaces and punctuation included.
- Concatenation keeps the argument order and inserts nothing.
- A substring is consecutive characters; its definition says where counting starts and what the second number means.
- Number the positions before applying any substring call.
- A string procedure returns a new string; the original stays the same unless reassigned.