AP · Computer Science Principles · Variables, Expressions and Strings

Strings and Substrings

11 min readUpdated December 1, 2026FreeAP Computer Science Principles, Algorithms and Programming

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

  1. 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.

  2. 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.

Position12345678
Characterbig(space)data

"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

How many characters are in "big data", and which is at position 4?
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

What does concat(concat("a", " "), "b") return?
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:

  1. Where does counting start? Usually position 1, but read it.
  2. Is the second number a length or a last position?

Take "planet" (p1 l2 a3 n4 e5 t6) and the numbers 2 and 3.

DefinitionCallResult
substring(str, start, length): length characters from position startsubstring("planet", 2, 3)"lan"
slice(str, first, last): positions first to last, both includedslice("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

With substring(str, start, length) counting from position 1, what is substring("notebook", 5, 4)?
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.

  1. Step 1: Trace each assignment.

    Lineabc
    1–2"cup""cake"
    3"cup""cake""cakecup"
    4"cup""cake""cakecups"
    Why

    Line 3 puts b first because it is the first argument; line 4 uses the current value of c.

  2. Step 2: Write what is displayed.

    "cakecups", without quotation marks.

    Why

    DISPLAY shows 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.

  1. 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.

    Why

    Fixed positions mean each part has a fixed start and length.

  2. 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)
    
    Why

    Building out one join at a time keeps each step easy to trace.

  3. Step 3: Trace it on "2027-03-15".

    day is "15", month is "03" and year is "2027", so out becomes "15.03.2027".

    Why

    A 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.

  1. Step 1: Number the positions.

    l1 i2 g3 h4 t5 h6 o7 u8 s9 e10.

    Why

    The definition counts from 1, so the numbering must too.

  2. Step 2: Apply the definition.

    Positions 3 to 6, both included, hold g, h, t and h.

    Why

    Here 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?

  1. 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".

  2. 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.

  1. 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 concat never adds.
  1. 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.
  1. 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.
  1. Exam-style word holds "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".
  1. 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:

  • n is 5. The first pass joins "tone" and "s": "tones".
  • The second pass works on "tones": "ones" and "t" give "onest".
  • The two DISPLAY calls show their values on one line, separated by a space.
  1. 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".
  1. Apply a holds "hot" and b holds "dog". What value does len(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.

Keep learning

Explore AP Computer Science Principles tutoring →

View the full Computer Science Principles syllabus →

More in this unit

Algorithms and Programming

All Variables, Expressions and Strings notes

Part of Novark's AP Computer Science Principles notes