Understanding
Strings
Explore how computers store and manipulate sequences of characters: from indexing and slicing to concatenation and common string operations.
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.
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.
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.
Core String Operations
The AP CSP exam pseudocode provides specific operations for working with strings. These are the building blocks for all string manipulation.
String Concatenation
Concatenation joins two or more strings end-to-end to create a new string. The original strings remain unchanged.
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.
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.
Example 1: Basic Operations
Example 2: Concatenation + Substring
Example 3: Loop Through a String
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.
Common Traversal Patterns
Many string problems involve iterating through each character. Here are the most common patterns you will encounter.
Common Mistakes
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
Consider the following pseudocode:
\(\texttt{word} \leftarrow \text{"SCIENCE"}\)
\(\texttt{part1} \leftarrow \texttt{substring(word, 1, 3)}\)
\(\texttt{part2} \leftarrow \texttt{substring(word, 5, 3)}\)
\(\texttt{result} \leftarrow \texttt{concat(part2, part1)}\)
What is the value of result?
Question 4 of 5
Consider the following pseudocode:
\(\texttt{text} \leftarrow \text{"ABCD"}\)
\(\texttt{result} \leftarrow \text{""}\)
FOR EACH character IN text
\(\texttt{result} \leftarrow \texttt{concat(character, result)}\)
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?