Python 3.04 - Strings HW
3.4 Strings — Interceptors
Completed the Popcorn Hacks and Homework for indexing/slicing, string methods, concatenation, formatting, and CSV-style string decoding. This version uses a pirate treasure-map theme.
Popcorn Hack 1: Extract the Hidden Message
Use indexing and slicing to recover the hidden words from the intercepted transmission.
Code Runner Challenge
Popcorn Hack 1 - Extract the hidden message using indexing and slicing
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Extract the hidden message using indexing and slicing
# Slice at known positions to pull out each hidden word.
message = "**TREASURE**--BURIED--**NORTH"
print("Word 1:", message[2:10])
print("Word 2:", message[14:20])
print("Word 3:", message[-5:])
print("Total length:", len(message))
print("Every other character:", message[::2])
Popcorn Hack 2: Scrub and Search a Noisy Transmission
Clean punctuation and whitespace, normalize the case, search the message, and count its words.
Code Runner Challenge
Popcorn Hack 2 - Clean and search a noisy transmission using string methods
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Clean and search a noisy transmission using string methods
# Trim the spaces, remove the junk characters, make it lowercase, then search and count.
noisy = " !!!Captain Jack spots: the TREASURE has been FOUND??? "
trimmed = noisy.strip()
print("Trimmed:", repr(trimmed))
cleaned = trimmed.replace("!", "").replace("?", "")
print("Cleaned:", cleaned)
lowered = cleaned.lower()
print("Lowered:", lowered)
print("Contains 'found':", "found" in lowered)
print("Word count:", len(cleaned.split()))
Popcorn Hack 3: Build a Decoded Report
Rebuild the decoded information using concatenation, type conversion, f-strings, and join().
Code Runner Challenge
Popcorn Hack 3 - Build a decoded report using concatenation and formatting
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - Build a decoded report using concatenation and formatting
# Store the pieces in variables, then combine them with +, an f-string, and join().
pirate = "Jack"
treasure = "GOLD"
coins = 120
clues = ["PALM", "TREE", "CAVE"]
headline = "Pirate " + pirate + " found: " + treasure
print(headline)
coin_line = "Coins: " + str(coins)
print(coin_line)
print(f"Coins: {coins}")
clue_text = "-".join(clues)
print("Joined clues:", clue_text)
print(f"REPORT: {headline} | {coin_line} | Clues: {clue_text}")
Homework Hack: Crack the Final Transmission
Decode the CSV-style transmission using splitting, indexing, slicing, replacement, searching, and formatted Markdown/JSON output.
Code Runner Challenge
Homework - Crack the Final Transmission using slicing, methods, and formatting
View IPYNB Source
# CODE_RUNNER: Homework - Crack the Final Transmission using slicing, methods, and formatting
# Strip the extra spaces, split on commas, then decode, search, and format the result.
raw = " 2026-10-05,JACK,THE-GOLD-IS-BURIED-NORTH,95 "
parts = raw.strip().split(",")
print("Segments:", parts)
date = parts[0]
pirate = parts[1]
code_text = parts[2]
certainty = parts[3]
print("Date:", date, "| Pirate:", pirate, "| Message code:", code_text, "| Confidence:", certainty)
print("Year check:", date[:4])
decoded = code_text.replace("-", " ")
print("Found 'gold':", "gold" in decoded.lower())
print(f"Markdown row: | {date} | {pirate} | {decoded} | {certainty}% |")
json_text = '{"date": "' + date + '", "pirate": "' + pirate + '", "message": "' + decoded + '", "confidence": ' + certainty + '}'
print("JSON line:", json_text)