Escape Room - Strings Lesson
Escape Room lesson on strings in JavaScript and Python.
Joke: Why did the programmer break up with String? → Because she had too many attachments.
🕵️♂️ String Heist — The Case of the Missing Character
A mischievous thief slipped into the data and swapped a character or hid a space. If you can’t spot the change, the scoreboard will name the wrong champion!
Quick sleuthing tasks:
- What does
"Hello".upper()return? (Bet an imaginary cookie.) - How many characters does
"abc \n"contain? Could the thief have left a newline as a clue?
Mini mission :
- List two sneaky ways characters hide in strings (example: trailing spaces, weird capitalization).
Why this matters:
- Small string quirks break real systems (logins, filenames, messages).
- Today you’ll learn quick fixes (trim, case-normalize, inspect escapes) so the thief can’t fool your code.
Teacher tip: Ask students for a funny example of a typo or stray space they’ve seen — celebrate the detective stories!
AP CSP 3.4: Strings — Interactive Lesson
Welcome to our lesson. In this lesson, you’ll explore how to work with strings in JavaScript and Python, with interactive challenges and instant feedback.
Why do we care about strings?
- Strings are everywhere: names, messages, passwords, data.
- You need to know how to read, change, and check them to build real apps.
Learning Goals:
- Understand string basics: length, concatenation, substring, case conversion, indexing.
- Practice AP CSP required string tasks.
- Get instant feedback and track your progress.
Quick Setup Check
- Make sure you can run Python code cells below.
- If you see errors, ask for help before continuing!
Interactive String Basics: Try It!
Below are some quick string challenges. Try each one and see instant feedback.
# Try It 1: What is the length of the string "Debuggers"?
from IPython.display import display, Javascript
import ipywidgets as widgets
q1 = widgets.Text(placeholder='Enter a number')
btn1 = widgets.Button(description='Check', button_style='info')
out1 = widgets.Output()
def check1(b):
with out1:
out1.clear_output()
if q1.value.strip() == '9':
print('✅ Correct! "Debuggers" has 9 letters.')
display(Javascript("localStorage.setItem('csp34_l2_try1','true');"))
else:
print('❌ Try again! Hint: Count each letter.')
btn1.on_click(check1)
display(widgets.HTML('<b>1. What is the length of the string "Debuggers"?</b>'), q1, btn1, out1)
HTML(value='<b>1. What is the length of the string "Debuggers"?</b>')
Text(value='', placeholder='Enter a number')
Button(button_style='info', description='Check', style=ButtonStyle())
Output()
# Try It 2: Concatenate two strings (self-contained)
from IPython.display import display, Javascript
import ipywidgets as widgets
q2a = widgets.Text(placeholder='First word')
q2b = widgets.Text(placeholder='Second word')
btn2 = widgets.Button(description='Check', button_style='info')
out2 = widgets.Output()
def check2(b):
with out2:
out2.clear_output()
a = q2a.value.strip()
b = q2b.value.strip()
if a and b:
no_space = a + b
with_space = a + ' ' + b
print(f"No space: {no_space}")
print(f"With space: {with_space}")
display(Javascript("localStorage.setItem('csp34_l2_try2','true');"))
else:
print('❌ Enter both words!')
btn2.on_click(check2)
display(widgets.HTML('<b>2. Concatenate two words of your choice:</b>'), q2a, q2b, btn2, out2)
HTML(value='<b>2. Concatenate two words of your choice:</b>')
Text(value='', placeholder='First word')
Text(value='', placeholder='Second word')
Button(button_style='info', description='Check', style=ButtonStyle())
Output()
# Try It 3: Extract a substring
q3 = widgets.Text(placeholder='Type a word')
q3_start = widgets.IntText(value=0, description='Start:')
q3_end = widgets.IntText(value=3, description='End:')
btn3 = widgets.Button(description='Check', button_style='info')
out3 = widgets.Output()
def check3(b):
with out3:
out3.clear_output()
word = q3.value.strip()
s, e = q3_start.value, q3_end.value
if word and 0 <= s < e <= len(word):
print(f'Substring: {word[s:e]}')
display(Javascript("localStorage.setItem('csp34_l2_try3','true');"))
else:
print('❌ Enter a word and valid start/end!')
btn3.on_click(check3)
display(widgets.HTML('<b>3. Extract a substring: Enter a word and start/end indices</b>'), q3, q3_start, q3_end, btn3, out3)
HTML(value='<b>3. Extract a substring: Enter a word and start/end indices</b>')
Text(value='', placeholder='Type a word')
IntText(value=0, description='Start:')
IntText(value=3, description='End:')
Button(button_style='info', description='Check', style=ButtonStyle())
Output()
# Try It 4: Convert to uppercase
q4 = widgets.Text(placeholder='Type a word')
btn4 = widgets.Button(description='Check', button_style='info')
out4 = widgets.Output()
def check4(b):
with out4:
out4.clear_output()
word = q4.value.strip()
if word:
print(f'Uppercase: {word.upper()}')
display(Javascript("localStorage.setItem('csp34_l2_try4','true');"))
else:
print('❌ Enter a word!')
btn4.on_click(check4)
display(widgets.HTML('<b>4. Convert a word to uppercase:</b>'), q4, btn4, out4)
HTML(value='<b>4. Convert a word to uppercase:</b>')
Text(value='', placeholder='Type a word')
Button(button_style='info', description='Check', style=ButtonStyle())
Output()
layout: post title: “Escape Room 3.4 - Level 2 String Searching” description: “Level 2 of the CSP 3.4 Escape Room - Master string searching and pattern matching” type: lesson toc: true comments: true permalink: /csp/escape-room/level2 author: Team Debuggers —
🧭 Navigation
📚 Advanced Concepts Mastered:
- Pattern Searching: Finding all occurrences of patterns in text
- Frequency Analysis: Counting and analyzing character frequencies
- Substring Operations: Finding common substrings and longest matches
- String Relationships: Rotations, anagrams, and pattern detection
- Algorithm Efficiency: Optimizing search and analysis operations
🎯 Reflection Questions:
- How would you optimize the pattern searching algorithm for very large texts?
- What real-world applications use frequency analysis?
- Can you think of other ways to detect if strings are rotations of each other?
- How might these algorithms be used in data compression or security?