Strings HW
Code Runner Challenge
coderunner 1
View IPYNB Source
# CODE_RUNNER: coderunner 1
transmission = "xxSIGNALxxxCONFIRMEDxx77xxDELTA"
# TODO 1: Use slicing to pull out "SIGNAL" (it starts at index 2 and is 6 characters long)
word_1 = transmission[2:8]
print("Word 1:", word_1)
# TODO 2: Use slicing to pull out "CONFIRMED" (it starts right after "SIGNALxxx")
word_2 = transmission[11:20]
print("Word 2:", word_2)
# TODO 3: Use NEGATIVE indexing/slicing to pull out "DELTA" (it is the last 5 characters)
word_3 = transmission[-5:]
print("Word 3:", word_3)
# TODO 4: Use len() to print how many characters are in the FULL transmission
print("Total length:", len(word_1 + word_2 + word_3))
# TODO 5: Use a slice with a step of 2 (transmission[::2]) to print every other character.
# Look closely: is any part of the hidden message still visible in this pattern?
stuff = transmission.replace(word_1,"")
stuff2 = stuff.replace(word_2, "")
stuff3 = stuff2.replace(word_3,"")
print("Every other character:", stuff3)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
coderunner 2
View IPYNB Source
# CODE_RUNNER: coderunner 2
noisy = " ...AGENT>>>Falcon---reports: the PACKAGE has ARRIVED!!! "
# TODO 1: Use .strip() to remove the leading/trailing whitespace. Save it as trimmed.
trimmed = noisy.strip()
print("Trimmed:", repr(trimmed))
# TODO 2: Use .replace() (you may need it more than once) to remove every ">", "-", and "!" character.
# Save the result as cleaned.
cleaned = trimmed.replace(">", "").replace("-", "").replace("!", "")
print("Cleaned:", cleaned)
# TODO 3: Use .lower() to make an all-lowercase COPY of cleaned. Save it as lowered.
lowered = cleaned.lower()
print("Lowered:", lowered)
# TODO 4: Use the `in` operator on lowered to check whether "arrived" appears anywhere in the message.
has_arrived = "arrived" in lowered
print("Contains 'arrived':", has_arrived)
# TODO 5: Use .split() with no arguments on cleaned to break it into a list of words.
# Save it as words, then print how many words there are with len().
words = cleaned.split()
print("Word count:", len(words))
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
coderunner 3
View IPYNB Source
# CODE_RUNNER: coderunner 3
agent_name = "Falcon"
target_word = "MIDNIGHT"
confidence = 87 # a percentage, stored as an int
fragments = ["ALPHA", "SEVEN", "DELTA"]
# TODO 1: Use + concatenation to build: "Agent Falcon intercepted: MIDNIGHT"
headline = "Agent " + agent_name + " intercepted: " + target_word
print(headline)
# TODO 2: confidence is an int. Use str() and + concatenation to build: "Confidence: 87%"
confidence_line = "Confidence: " + str(confidence) + "%"
print(confidence_line)
# TODO 3: Rewrite the SAME line as confidence_line_fstring, but using an f-string this time
confidence_line_fstring = f"Confidence: {confidence}%"
print(confidence_line_fstring)
# TODO 4: Use "-".join(fragments) to combine the fragments list into one string
joined_fragments = "-".join(fragments)
print("Joined fragments:", joined_fragments)
# TODO 5: Use an f-string to build ONE final report line combining headline, confidence_line,
# and joined_fragments, in the form:
# "REPORT: <headline> | <confidence_line> | Fragments: <joined_fragments>"
final_report = f"REPORT: {headline} | {confidence_line} | Fragments: {joined_fragments}"
print(final_report)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see 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
raw_transmission = " 2026-09-15,GHOST,THE-EAGLE-LANDS-AT-DAWN,87 "
# TODO 1: Use .strip() to remove the outer whitespace, then .split(",") to break it into fields.
segments = raw_transmission.strip().split(",")
print("Segments:", segments)
# TODO 2: Use indexing on segments to pull out the date, agent, message code, and confidence.
date = segments[0]
agent = segments[1]
message_code = segments[2]
confidence_str = segments[3]
print("Date:", date, "| Agent:", agent, "| Message code:", message_code, "| Confidence:", confidence_str)
# TODO 3: Use SLICING (not a method) on date to confirm it starts with the year "2026".
year_check = date[0:4]
print("Year check:", year_check)
# TODO 4: Use .replace() to turn the dashes in message_code into spaces. Save it as message.
message = message_code.replace("-", " ")
# TODO 5: Use .lower() and the `in` operator to check whether "dawn" appears anywhere in message.
found_dawn = "dawn" in message.lower()
print("Found 'dawn':", found_dawn)
# TODO 6: Use an f-string with "|" separators to build a Markdown table row of the decoded fields,
# in the form: "| <date> | <agent> | <message> | <confidence_str>% |"
markdown_row = f"| {date} | {agent} | {message} | {confidence_str}% |"
print("Markdown row:", markdown_row)
# TODO 7: Use an f-string to build a JSON-style line for the same fields, mixing "{", "}", ":", and ","
# the way real systems pass structured data. Use str(found_dawn).lower() for the boolean.
# Build the JSON by separating the braces from the content
open_brace = "{"
close_brace = "}"
json_line = open_brace + f'"date": "{date}", "agent": "{agent}", "message": "{message}", "confidence": {confidence_str}, "contains_dawn": {str(found_dawn).lower()}' + close_brace
print("JSON line:", json_line)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...