Topic 3.4: Strings | AP CSP Big Idea 3 | APCSExamPrep.com
Strings
After this lesson, you will be able to:
- Explain that a string is an ordered sequence of characters, including spaces
- Concatenate strings and variables with + to build output
- Find the length of a string with len(), counting every character
- Convert a number to a string with str() before joining it to text
- Distinguish numeric addition from string concatenation when using +
- Assemble clear, labeled output for your Create Performance Task
Every message a program shows you is a string that someone built. When a game says Player Ada scored 95, that line was assembled from fixed text, a name, and a number glued together in order. Get one space wrong and it reads PlayerAdascored95. Forget one conversion and the program crashes. This lesson is about joining pieces of text cleanly and measuring how long they are.
What a String Is
A string is an ordered sequence of characters: letters, digits, spaces, and punctuation, kept in order and treated as text. In Python you write a string inside quotation marks, like "Hello" or "grade: A". The order matters, so "ab" and "ba" are different strings. Because a string is text, the characters inside it are not treated as numbers even when they look like digits: "7" is the character seven, not the value 7.
The two string operations you must know for AP CSP are concatenation (joining strings together) and finding a string's length (how many characters it contains). Everything in this lesson is built from those two ideas plus one conversion rule for mixing numbers into text.
The CED (AAP-2.D) expects you to build output by joining strings and variables and to find a string's length. AP pseudocode keeps string handling limited, mainly concatenation and length, so the exam stays at the level of assembling text, not slicing it apart.
Concatenation: Joining Strings with +
Concatenation uses the + operator to glue two strings into one longer string, in order, left to right. The result contains every character of the first string followed by every character of the second. Nothing is added between them, so if you want a space, you must include it yourself.
name = "Ada" greeting = "Hello, " + name print(greeting)
name <- "Ada" greeting <- "Hello, " + name DISPLAY(greeting)
Here "Hello, " + name produces "Hello, Ada". The space and comma are part of the first string, so they appear in the result. This is how programs build a message out of fixed text and a variable that changes.
With numbers, + adds: 2 + 3 is 5. With strings, + concatenates: "2" + "3" is "23", not 5. Python decides which behavior to use based on the types of the values, not on how they look. Two characters that look like digits are still text, so they are joined, not summed.
x = "2" + "3" print(x)
Mixing Numbers and Text: str()
You often need to put a number inside a message, like a score or a count. But Python will not concatenate a string and a number directly. Writing "score: " + 7 raises a TypeError, because one side is text and the other is a number and Python refuses to guess what you meant. The fix is to convert the number to a string first with str().
score = 95 label = "Score: " + str(score) print(label)
score <- 95 label <- "Score: " + score DISPLAY(label)
The call str(score) turns the number 95 into the text "95", which can then be concatenated. On the pseudocode side, AP pseudocode is looser about combining text and numbers, but in real Python the conversion is required. Whenever you build a message that includes a number, wrap the number in str().
Any time you join text with a number using +, you must convert the number: "total: " + str(count). Leaving out str() is one of the most common runtime errors on string output. The moment you see + between a quoted string and a number, reach for str().
n = 7 print("score: " + n)
Length: Counting Every Character
The length of a string is how many characters it contains. In Python you find it with len(); AP describes this as finding the length of the string. The count includes every character: letters, digits, punctuation, and spaces all count. Only the quotation marks are left out, because they mark where the string begins and ends and are not part of the text.
word = "banana" print(len(word))
word <- "banana" DISPLAY(LENGTH(word))
The string "banana" has 6 characters, so len(word) is 6. Watch spaces carefully: "go team" has 7 characters because the space between the words counts. A blank space is a real character, exactly like a letter.
When a length question includes spaces, count them. "a b c" has 5 characters: three letters and two spaces. Forgetting to count spaces is the number one mistake on length questions, so slow down and tick off each character, blanks included.
s = "a b" print(len(s))
Building Output from Pieces
Real programs assemble their output from several parts: fixed text, variables, and numbers converted with str(). You build the whole message by concatenating those pieces in the right order, remembering to add spaces and punctuation yourself.
a = "AP" b = "CSP" print(a + b) print(a + " " + b)
a <- "AP" b <- "CSP" DISPLAY(a + b) DISPLAY(a + " " + b)
The first print joins "AP" and "CSP" with nothing between them, so it shows APCSP. The second inserts " " between them, so it shows AP CSP. The only difference is one space that you chose to include. Building a clean output string is just deciding, piece by piece, exactly which characters belong and in what order.
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| String | An ordered sequence of characters treated as text | "grade: A" |
| Concatenation | Joining two strings into one with the + operator | "Hi " + name |
| Length | The number of characters in a string, spaces included |
len("go team") is 7 |
| str() | Converts a number to its string form so it can be concatenated |
str(95) is "95"
|
| TypeError | The error raised when a string and a number are joined without str() | "n: " + 7 |
| Character | A single item in a string, including a space | the space in "a b"
|
Almost every Create Task program produces output a person will read, and that output is a string you build. Concatenation and str() are how you turn raw variables into a clear, labeled message instead of a bare number. Clean output makes your program easier to explain in your written response.
A point-earning example
Imagine a program that reports a player's result by joining fixed text with two variables:
In real Python that score would be wrapped in str(). In your written response, describe how the output is assembled:
- "The program builds the output by concatenating the fixed text with the variables
nameandscore." - "Because
scoreis a number, it is converted withstr()before being joined to the text, so the message displays correctly." - "Spaces are included inside the quoted pieces so the words in the final message are separated."
The trap to avoid
Do not concatenate a number to text without str(), and do not forget the spaces between pieces. A missing str() crashes the program, and a missing space produces run-together output like PlayerAdascored95. Both cost you clarity. See the full Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
23 (the two characters), and not the number 5?- I. print("2" + "3")
- II. print(str(2) + str(3))
- III. print(2 + 3)
name is set. Print a greeting by joining "Hello, " with the name. Target output: Hello, Ada
score is a number. Print Score: a space, and the score. Remember to convert the number with str(). Target output: Score: 95
word is set. Print the number of characters in it using len(). Target output: 6
first and last are set. Print the full name with a single space between the two parts. Target output: Ada Lovelace
word is set. Print a sentence that states the word and how many letters it has, using len() and str(). Target output: computer has 8 letters
name, score, and grade, build and print one formatted line in exactly this form: Name: Ada, Score: 95, Grade: A. The score is a number, so convert it. Only the three inputs are given. Target output: Name: Ada, Score: 95, Grade: A
- join
firstandlastinto a full name with a space - add
pointsandbonusinto a numeric total - print two lines,
Player: Grace HopperandTotal: 50
Frequently Asked Questions
"Hello". The order matters, and characters that look like digits, such as "7", are still text, not numbers.+ operator. The characters of the first string are followed by the characters of the second, with nothing added between them, so you must include any spaces yourself.TypeError. Convert the number first with str(), as in "score: " + str(7).len() counts every character, including spaces and punctuation. The string "go team" has a length of 7 because the space between the words counts. Only the surrounding quotation marks are not counted.str() to include numbers and adding spaces between pieces gives clear, labeled output that is easy to describe in your written response.🔗 Continue studying
The Superpack includes an editable Topic 3.4 slide deck with animated concatenation and length traces, a str() and spacing bug bank, a print-ready output-building worksheet, and a unit quiz. View what's included →
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]