What are Strings?
Strings, as mentioned earlier, are an ordered list of characters. You can navigate through them using index values, just like how you can navigate through a list using index values.
Although you can't perform mathematical operations with strings, you can still manipulate them.

Slicing
A substring is a part of an existing string. For example, "APcomputer" would be a substring of the string "APcomputerscienceprinciples." The process of creating a substring is known as slicing.
There are many different ways to slice a string: here's a basic example.
string_example1 = "APcomputerscienceprinciples" print (string_example1[0:10])
This code will print the substring "APcomputer". In Python, index values begin at 0, and the character at the end index number (10) is not included.
Here's another example of slicing a string.
string_example2 = "APcomputerscienceprinciples" print (string_example[2:9])
This code returns the substring "compute".
Concatenation
You can also concatenate strings. String concatenation occurs when two or more strings are joined end to end to make a new string. This is usually represented by the + symbol.
part_one = "Hello" part_two = "_World!" print (part_one + part_two)
The code returns:
Hello_World!
Frequently Asked Questions
How do I concatenate strings in Java using the `+` operator?
In Java, the + operator joins (concatenates) strings end-to-end to make a new string (EK AAP-2.D.1). If either operand is a String, + converts the other operand to text and concatenates. Examples: - "Hello, " + name // joins literal + variable - "a" + "b" // "ab" - "Score: " + 10 // "Score: 10" Order matters when mixing numbers and strings: 1 + 2 + "3" gives "33" (because 1+2 evaluated first), while "1" + 2 + 3 gives "123" (left-to-right string conversion). Strings are immutable, so each + produces a new String object (useful to know for performance but fine for AP-level problems). On the AP exam you may be asked to evaluate such expressions (LO AAP-2.D). For a quick review, see the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
What's the difference between `String` concatenation and using `StringBuilder`?
String concatenation (using + or concat) creates a new String each time you join text because Strings are immutable (you can’t change the original). That’s fine for a few joins (EK AAP-2.D.1), but inside loops or many repeated joins it’s slow and uses more memory. StringBuilder is a mutable object designed for efficient, repeated modification: you append, insert, or delete without making a new object each time, then call toString() when you’re done. Use concatenation for simple expressions and readability; use StringBuilder when performance or many edits matter (e.g., building a long output in a loop). This distinction ties to AP Topic 3.4 concepts like immutability and concatenation operator and is worth knowing for code-analysis questions on the exam (LO AAP-2.D). For a quick refresher, check the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
Why am I getting a `StringIndexOutOfBoundsException` when I try to use `substring()`?
You get StringIndexOutOfBoundsException when substring() is given an index that isn’t inside the string. Remember Java (and the AP CED) uses zero-based indexing: the first character is index 0 and the last character is index length() - 1. Common mistakes: - Using substring(start) with start < 0 or start > s.length() (start == s.length() is invalid for substring(start) if you expect a character). - Using substring(start, end) where start < 0, end > s.length(), or start > end. The end parameter is exclusive (it stops just before end). Example: for "hello" (length 5) valid calls include substring(0,3) -> "hel" and substring(2) -> "llo". substring(5) or substring(3,6) throws the exception. Check your indices with s.length() or charAt() if you need single chars. For a quick refresher on substring, indexing, and other string ops (EK AAP-2.D.2, charAt, length), see the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw). For extra practice, Fiveable has lots of practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
How do I write the syntax for the `substring()` method correctly?
Use zero-based indexing and remember the end index is exclusive. Typical Java/JavaScript-style syntax: - s.substring(start)—returns from index start to the end. - s.substring(start, end)—returns characters from index start up to but not including index end. Rules to keep in mind (AP CED keywords: zero-based indexing, substring, charAt, length, immutability): - Indexing starts at 0, so the first character is s.charAt(0). - start must be >= 0 and <= s.length(); end must be between start and s.length(). - The returned substring is a new string (strings are immutable). Examples: - "hello".substring(1) → "ello" - "hello".substring(1,4) → "ell" (chars at indices 1,2,3) For more practice on string expressions and substring use, check the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and try practice problems at (https://library.fiveable.me/practice/ap-computer-science-principles).
What's the difference between `charAt()` and `substring()` methods?
charAt() and substring() both access parts of a string, but they do different things: - charAt(index) returns a single character (a char) at the given zero-based index. Example: s.charAt(2) gives the character at position 2. Use when you only need one character. Out-of-range index causes an error. - substring(startIndex, endIndex) returns a new string that is a substring of the original. startIndex is inclusive, endIndex is exclusive (zero-based). Example: s.substring(2, 5) gives characters at indices 2, 3, and 4. To get a one-character string you can use s.substring(i, i+1). Strings are immutable, so substring returns a new string and doesn’t change the original. Both appear in the CED keywords (charAt, substring, character indexing, zero-based indexing, immutability). For more practice and examples, check the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and Fiveable’s practice questions (https://library.fiveable.me/practice/ap-computer-science-principles).
When should I use `String.equals()` vs `==` to compare strings?
Use String.equals() to check whether two strings have the same sequence of characters (value equality). Use == only to check whether two string variables refer to the exact same object in memory (reference/identity). Why it matters: Java stores string literals in a shared pool, so two literals with the same text may be the same object (== true) but strings created at runtime (concat, substring, user input) often aren’t—equals() still returns true if their text matches. Because strings are immutable, equals() is the reliable way to test content. Tips: avoid NullPointerException by using a safe call like "constant".equals(var) or Objects.equals(a,b). On the AP exam you’ll be asked to evaluate expressions that manipulate strings (LO AAP-2.D); expect content comparisons with equals(). For more review, see the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and practice questions (https://library.fiveable.me/practice/ap-computer-science-principles).
I don't understand why my string comparison always returns `false` even when they look the same - can someone help?
Most likely your code is comparing the wrong thing—not the actual characters in the strings. Common causes: - In Java: using == compares object references, not contents. Use myStr.equals(otherStr) (or equalsIgnoreCase) to compare characters. - In JavaScript: "==" does type-coercion; use "===" for strict equality (but both compare contents for strings—only use ===). - Hidden differences: extra whitespace/newlines, different letter case, or escape/unicode differences. Try printing lengths and each char (charAt / indexing) to spot mismatches. - Building strings: concatenation order or accidental substring/trim omissions can change content. Remember strings are immutable—methods like toLowerCase(), trim(), replace return new strings. This is exactly the kind of thing covered in Topic 3.4 (LO AAP-2.D)—check substring, charAt, length, immutability. For a quick refresher see the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and drill problems at Fiveable practice (https://library.fiveable.me/practice/ap-computer-science-principles).
How do I trace through code that uses multiple `substring()` calls step by step?
Think of tracing multiple substring() calls like tracking a value through a chain of short programs. Quick checklist you can use every time: 1. Write the original string and label each character with its zero-based index (0,1,2,...). (EK AAP-2.D.2, zero-based indexing) 2. Evaluate the first substring(start, end): copy the characters from start up to (but not including) end. Write the resulting string and give it new indices starting at 0. 3. For the next substring call, apply start/end to the current string’s indices (not the original). Repeat: extract characters, write the new string, re-index. 4. If substring is called with one arg (start), take from start to length (use length()). Remember strings are immutable so each call produces a new string. 5. Check edge cases: start==end -> empty string, start 0 -> whole string, out-of-range indices would be an error. Example: s = "HELLO" t = s.substring(1,4) -> "ELL" (indices 0:'E',1:'L',2:'L') u = t.substring(1) -> "LL" (takes index 1 to end) This is exactly the sort of expression evaluation you’ll see on the AP exam (LO AAP-2.D). For more examples and practice tracing problems, see the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and use Fiveable’s practice questions (https://library.fiveable.me/practice/ap-computer-science-principles).
What's the syntax for finding the length of a string using `length()`?
Use the string’s length() method like this: myString.length() Example in a Java-style snippet: String s = "hello"; int n = s.length(); // n is 5 Notes: length() is a method that returns an integer equal to the number of characters (including spaces and punctuation). This matches the CED keywords (char indexing, length) in Topic 3.4—you’ll see questions that expect you to evaluate expressions like this on the AP exam. If you’re using JavaScript, remember it’s a property there (s.length without parentheses), so check your language. For more on strings and practice, see the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and grab extra practice problems at (https://library.fiveable.me/practice/ap-computer-science-principles).
Can someone explain how string indexing works in Java - I'm confused about starting at 0?
Think of a Java string like a row of boxes holding characters. Java uses zero-based indexing, so the first box is index 0, the second is index 1, etc. That means: - s.charAt(0) returns the first character. - s.length() gives the number of characters; the last character is at index s.length() - 1. - s.substring(a, b) returns characters from index a up to (but not including) index b—so substring(1,4) extracts indices 1,2,3. Example: String s = "Hello"; - s.charAt(0) -> 'H' - s.charAt(4) -> 'o' - s.substring(1,4) -> "ell" Remember strings are immutable (methods return new strings), and off-by-one errors are the most common bug when slicing indices. These are exactly the kinds of string-expression skills the AP CSP exam tests in Topic 3.4 (EK AAP-2.D.1/2.D.2). For a quick review, check the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and try practice problems at (https://library.fiveable.me/practice/ap-computer-science-principles).
How do I write code to extract a specific part of a string using `substring()`?
Use substring(start, end) to get the part of a string from index start up to (but not including) index end. Remember strings use zero-based indexing and are immutable—substring returns a new string, it doesn’t change the original. Examples (Java/JavaScript-style): - let s = "AP COMPUTER SCIENCES"; s.substring(0,2) // "AP" (characters at indices 0 and 1) s.substring(3) // "COMPUTER SCIENCES" (from index 3 to end) Common patterns for AP problems: - Extract a fixed part: substring(5, 8) grabs 3 chars (indices 5,6,7). - Use indexOf to find dynamic positions: let i = s.indexOf(" "); s.substring(0, i) gets the first word. - Use length to get the end: s.substring(s.length()-4) gets the last 4 chars. These are exactly the kinds of string-manipulation expressions the CED tests (charAt, substring, indexOf, length). For more examples and practice aligned to Topic 3.4, see the Fiveable study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and thousands of practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
What happens when I concatenate a string with an integer in Java?
In Java, using the + operator with a String and an int performs string concatenation: the int is converted to its string form and joined end-to-end with the String. For example: "Age: " + 18 yields "Age: 18". Because concatenation is left-to-right, expressions matter: 1 + 2 + "3" -> "33" (1+2 done first), while "1" + 2 + 3 -> "123". Strings are immutable, so concatenation creates a new String rather than changing the originals. If you need explicit conversion, use Integer.toString(n) or String.valueOf(n). This behavior is part of LO AAP-2.D (string concatenation) on the AP CSP CED—review Topic 3.4 in the study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles) to get comfortable with ordering and types.
Why does `substring(2, 5)` give me different characters than I expected?
Most likely you ran into zero-based indexing and the “end is exclusive” rule. In languages/AP reference-sheet APIs (charAt, substring), characters are numbered starting at 0. substring(a, b) returns characters from index a up to—but NOT including—index b. So substring(2, 5) gives characters at indices 2, 3, and 4 (three chars), not 2–5. Example: "computer" - indices: c(0) o(1) m(2) p(3) u(4) t(5) e(6) r(7) - "computer".substring(2,5) → "mpu" (chars at 2,3,4) If you wanted 4 characters starting at index 2, use substring(2, 6). Use length() to avoid out-of-range errors. This is a common off-by-one gotcha tied to EK AAP-2.D.1/2.D.2 (char indexing, substring). For a quick refresher, check the Topic 3.4 Strings study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
What are common string methods I need to know for the AP Computer Science A exam?
For AP CSP Topic 3.4 you should be comfortable reading and evaluating expressions that use these common string operations (these match LO AAP-2.D on the CED): - Concatenation (join strings with + or concat) - length()—get number of characters (zero-based indexing matters) - charAt(index) or direct indexing—access a character (remember index 0 is first) - substring(start, end)—extract part of a string (end usually exclusive) - indexOf(sub)—find where a substring starts (returns -1 if not found) - replace(old, new)—substitute characters or substrings - split(delimiter)—break into parts (useful for parsing) - escape sequences (e.g., "\n", "\"") and string literals - immutability—string operations make new strings, don’t change the original - Unicode awareness—characters may be multi-byte in some contexts AP exam questions will ask you to evaluate expressions combining these (EK AAP-2.D.1/2). Review worked examples on the Topic 3.4 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw) and drill practice problems (https://library.fiveable.me/practice/ap-computer-science-principles) so you can quickly spot off-by-one and -1 index cases.
I'm confused about immutable strings - what does that mean and why does it matter?
Immutable means a string’s characters can’t be changed after the string is created. When you do concatenation, substring, replace, or use methods like charAt and indexOf, those operations produce new strings rather than modifying the original one. For AP CSP (LO AAP-2.D / EK AAP-2.D.1–2), that matters because: - You can’t change a character by index—you’ll need to build a new string (or use a mutable buffer in some languages) to get a modified result. - Each string operation can create new values in memory, so repeated concatenation in a loop can be slower and use more memory. - Understanding immutability helps you predict outputs of expressions (important for exam questions that evaluate string-manipulating expressions). Review string methods (substring, charAt, indexOf, length, replace, split) and think in terms of “returns a new string” when you practice. For the AP study guide see the Topic 3.4 strings page (https://library.fiveable.me/ap-computer-science-principles/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw). For extra practice problems, check Fiveable’s practice set (https://library.fiveable.me/practice/ap-computer-science-principles).