01 / 21

Understanding
Strings

Explore how computers store and manipulate sequences of characters: from indexing and slicing to concatenation and common string operations.

AP Computer Science Principles

What Is a String?

A string is an ordered sequence of characters. Characters can be letters, digits, symbols, or even spaces. In most programming languages, strings are enclosed in quotation marks.

Position: 1 2 3 4 5 6 H E L L O ! Value:
\(\text{Length} = 6\) characters

Why Strings Matter

Strings are one of the most commonly used data types in programming. Nearly every application processes text: usernames, messages, search queries, file paths, and more.

User Input
Forms, search bars, and text fields all capture strings from users.
Data Storage
Names, addresses, and messages are stored as strings in databases.
Communication
APIs and web services send and receive data as formatted strings (JSON, XML).

How Strings Are Represented

At the lowest level, each character in a string is stored as a number using a character encoding like ASCII or Unicode. Your program sees characters, but the computer sees numbers.

Character ASCII Value Binary H 72 01001000 e 101 01100101 l 108 01101100 (space) 32 00100000

Core String Operations

The AP CSP exam pseudocode provides specific operations for working with strings. These are the building blocks for all string manipulation.

1
Length: Find how many characters are in a string using \(\texttt{len(str)}\).
2
Concatenation: Join two strings together using the \(+\) operator or \(\texttt{concat(str1, str2)}\).
3
Substring: Extract a portion using \(\texttt{substring(str, start, length)}\).
4
Indexing: Access a single character at a given position (index starts at 1 in AP pseudocode).

String Concatenation

Concatenation joins two or more strings end-to-end to create a new string. The original strings remain unchanged.

str1 A P + str2 C S P result A P C S P
"AP CSP" (\(\text{length} = 6\))

Extracting a Substring

A substring is a contiguous portion of a string. In AP CSP pseudocode: \(\texttt{substring(str, startPos, numChars)}\) returns numChars characters beginning at startPos.

Position: 1 2 3 4 5 6 7 S T R I N G S "RIN"
\(\texttt{substring("STRINGS", 3, 3)}\)

Strings vs Lists

Strings and lists are both ordered sequences, but they differ in key ways. Understanding these differences prevents common exam mistakes.

Strings Lists
Elements Characters only Any data type
Mutability Often immutable Usually mutable
Notation "Hello" [1, 2, 3]
Concatenation \(\text{"A"} + \text{"B"} = \text{"AB"}\) \([1] + [2] = [1, 2]\)
Length Number of characters Number of elements

How to Solve String Problems

Follow this systematic approach when solving string problems on the AP CSP exam.

1
Identify the string: Write out every character, including spaces and punctuation.
2
Number each position: Label positions starting at 1 (AP pseudocode convention).
3
Apply the operation: Use \(\texttt{len()}\), \(\texttt{concat()}\), or \(\texttt{substring()}\) and trace the result step by step.
4
Count carefully: Verify the length of your result matches what you expect.
5
Check edge cases: Empty strings, single characters, and off-by-one positions.

Example 1: Basic Operations

str ← "COMPUTER" // What is len(str)? // What is substring(str, 4, 3)?
1
Write it out: C-O-M-P-U-T-E-R (positions 1 through 8).
2
\(\texttt{len(str)}\): Count all characters: 8.
3
\(\texttt{substring(str, 4, 3)}\): Start at position 4 (P), take 3 chars: P-U-T = "PUT".

Example 2: Concatenation + Substring

first ← "CODE" second ← "BREAK" result ← concat(substring(first, 1, 2), substring(second, 4, 2)) // What is result?
1
\(\texttt{substring(first, 1, 2)}\): Start at position 1 of "CODE", take 2 chars: "CO".
2
\(\texttt{substring(second, 4, 2)}\): Start at position 4 of "BREAK", take 2 chars: "AK".
3
\(\texttt{concat("CO", "AK")}\): Join them: "COAK".

Example 3: Loop Through a String

word ← "DATA" newStr ← "" FOR EACH character IN word { newStr ← concat(character, newStr) } // What is newStr?
1
Iteration 1: character = "D", \(\texttt{newStr} = \texttt{concat("D", "")}\) = "D"
2
Iteration 2: character = "A", \(\texttt{newStr} = \texttt{concat("A", "D")}\) = "AD"
3
Iteration 3: character = "T", \(\texttt{newStr} = \texttt{concat("T", "AD")}\) = "TAD"
4
Iteration 4: character = "A", \(\texttt{newStr} = \texttt{concat("A", "TAD")}\) = "ATAD"

String Immutability

In many languages (and in AP CSP), strings are immutable: you cannot change a character in place. Instead, you create a new string from parts of the old one.

original: C A T S Can't change "T" directly! new:
\(\texttt{concat(concat(substring(original, 1, 2), "R"), substring(original, 4, 1))}\)
C A R S

Common Traversal Patterns

Many string problems involve iterating through each character. Here are the most common patterns you will encounter.

Count
Count how many times a specific character appears in a string.
Search
Find the position of the first occurrence of a character or substring.
Transform
Build a new string by modifying each character (e.g., shift for encryption).

Common Mistakes

✕ Off-By-One Indexing
Forgetting that AP pseudocode starts at position 1, not 0. \(\texttt{substring("HELLO", 1, 1)}\) returns "H", not "E".
✕ Forgetting Spaces
Not counting space characters. "HI THERE" has 8 characters, not 7. The space at position 3 counts.
✕ Mutating Strings
Trying to change a character in place. Strings are immutable: you must build a new string using \(\texttt{concat}\) and \(\texttt{substring}\).
✕ Concat Order
Swapping the order in \(\texttt{concat(a, b)}\). \(\texttt{concat("AB", "CD")}\) = "ABCD", but \(\texttt{concat("CD", "AB")}\) = "CDAB".

Operations Summary

Operation Syntax Example Result
Length \(\texttt{len(str)}\) \(\texttt{len("CODE")}\) 4
Concat \(\texttt{concat(a, b)}\) \(\texttt{concat("HI", "!")}\) "HI!"
Substring \(\texttt{substring(str, s, n)}\) \(\texttt{substring("APPLE", 2, 3)}\) "PPL"
Access Character \(\texttt{substring(str, i, 1)}\) \(\texttt{substring("HELLO", 3, 1)}\) "L"

Question 1 of 5

What is the value of \(\texttt{len("AP CSP")}\)?

Question 2 of 5

What does \(\texttt{substring("PROGRAM", 4, 3)}\) return?

Question 3 of 5

What is the value of result?

Question 4 of 5

What is the value of result after the loop completes?

Question 5 of 5

Which expression correctly extracts the last character of a string variable s?