Topic 3.4: Strings | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.4 Strings
3.4
Big Idea 3 • Algorithms & Programming

Strings

🕐 ~35 min FREE 📖 6 MCQ questions 🎮 String Builder game 💻 Live Python editor AAP-2.D

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
📈 Exam weight: Strings appear throughout Big Idea 3 tracing and spot-the-bug items, and building output is part of almost every Create Task. Concatenation versus addition, missing str(), and length that ignores spaces are favorite trap topics.
💡 Think about this first

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.

🎯 What the exam expects

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.

⚠ The same + means two different things

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.

✍ Mini Exercise 1 • Predict the output
Both values are strings. What is printed?
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().

⚠ Forgetting str() is a classic error

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().

✍ Mini Exercise 2 • Spot the flaw
Why does this segment raise an error instead of printing?
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.

🎯 Spaces are characters too

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.

✍ Mini Exercise 3 • Fill in the blank
Type the exact number this prints.
s = "a b"
print(len(s))
prints:

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"
📋 Create Performance Task • Building readable output with strings

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:

name <- "Ada" score <- 95 message <- "Player " + name + " scored " + score DISPLAY(message)

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 name and score."
  • "Because score is a number, it is converted with str() 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 →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: is there a space between the two words?
What does this code segment display?
p = "Hi" q = "there" print(p + q)
Incorrect. No space was included in either string, so the words are joined with nothing between them.
Correct. Concatenation joins exactly the characters given. Neither string contains a space, so the result is Hithere.
Incorrect. The + is the concatenation operator; it does the joining and is not itself printed.
Incorrect. The values of p and q are joined, not the variable names.
Question 2 of 6Spot the bug
Predict first: what are the two types being joined?
This segment is meant to print a labeled number, but it raises an error. What is the cause?
n = 7 print("score: " + n)
Incorrect. print can display a joined string fine; the error happens before print, while building the value.
Incorrect. A colon inside a string is ordinary text and is completely legal.
Correct. One side is text and the other is a number, so Python raises a TypeError. Writing str(n) fixes it.
Incorrect. n is meant to be the number 7; quoting it would make it text but is not what the error is about.
Question 3 of 6Spot the bug
This is meant to display a first and last name with a space between them. What is wrong?
a = "Ada" b = "Lovelace" print(a + b)
Incorrect. No space character appears in either string, so the names are not separated.
Correct. Concatenation adds nothing between the pieces. Since no space was included, it displays AdaLovelace. You would write a + " " + b.
Incorrect. len only counts characters; it does not add a space. You add a space by concatenating " ".
Incorrect. The order is fine; the only problem is the missing space between the names.
Question 4 of 6I, II, III
Which of the following display the string 23 (the two characters), and not the number 5?
  • I. print("2" + "3")
  • II. print(str(2) + str(3))
  • III. print(2 + 3)
Incorrect. I displays 23, but II also does, because str(2) and str(3) are text and are concatenated.
Correct. I joins two string characters into 23. II converts both numbers to text first, so they concatenate to 23. III adds two numbers and displays 5.
Incorrect. III uses numeric addition, so it displays 5, not 23.
Incorrect. III is number addition and displays 5, so it does not belong.
Question 5 of 6NOT question
Each segment builds a short string. Which one does NOT include a space in its output?
Incorrect. A space string is concatenated in the middle, so the output is a b, which has a space.
Incorrect. The first piece ends with a space, so the output is a b, which has a space.
Correct. Neither piece contains a space and none is added between them, so the output is ab with no space.
Incorrect. The second piece begins with a space, so the output is a b, which has a space.
Question 6 of 6Trace
Predict first: do the spaces count as characters?
What does this code segment display?
s = "go team" print(len(s))
Incorrect. That misses the space. Count every character, blanks included.
Correct. g, o, space, t, e, a, m is 7 characters. The space between the words counts.
Incorrect. There are 6 letters and 1 space, which is 7, not 8. The quotation marks are not counted.
Incorrect. len counts characters, not words. There are 7 characters.
🎮 Lesson Game
String Builder
Trace each string operation and predict exactly what it displays. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • what string is built?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • concatenate a greeting
name is set. Print a greeting by joining "Hello, " with the name. Target output: Hello, Ada
Level 2 • str()
Problem 2 of 8 • put a number in text
score is a number. Print Score: a space, and the score. Remember to convert the number with str(). Target output: Score: 95
Level 3 • Length
Problem 3 of 8 • count the characters
word is set. Print the number of characters in it using len(). Target output: 6
Level 4 • Fix the bug
Problem 4 of 8 • add the missing str()
This should print a label with a number, but it raises a TypeError. Fix it so the number is converted before it is joined. Target output: age: 12
Level 5 • Build with spaces
Problem 5 of 8 • first and last name
first and last are set. Print the full name with a single space between the two parts. Target output: Ada Lovelace
Level 6 • Create Task style
Problem 6 of 8 • mix text, number, and length
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
Level 7 • Challenge
Problem 7 of 8 • you write the whole program
Open ended. Write the entire program yourself. Given 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
Level 8 • Challenge
Problem 8 of 8 • assemble output across steps
Open ended. Write the entire program yourself. Work in steps:
  • join first and last into a full name with a space
  • add points and bonus into a numeric total
  • print two lines, Player: Grace Hopper and Total: 50
Note that + joins the names as text but adds the numbers. Target output: Player: Grace Hopper then Total: 50

Frequently Asked Questions

A string is an ordered sequence of characters treated as text, written inside quotation marks, like "Hello". The order matters, and characters that look like digits, such as "7", are still text, not numbers.
Concatenation is joining two strings into one longer string with the + 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.
Because one value is a string and the other is a number, and Python will not concatenate different types. It raises a TypeError. Convert the number first with str(), as in "score: " + str(7).
Yes. 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.
Your program's output is a string you build by concatenating fixed text with your variables. Using str() to include numbers and adding spaces between pieces gives clear, labeled output that is easy to describe in your written response.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]