The Tag Hunter
Hoppy stops at a scout wall and finds a wind-bent note where sentences, warnings, and directions are all mixed together. Only a few short tags are clearly held inside square brackets.
This is a common text-detective moment: the text looks messy, but the signal is still there. If you can see where a tag begins and where it ends, you can pull those short clues out first.
A tag is not a parser problem when the boundaries are obvious
Some mixed text does not need complex parsing. If the tags are flat and the boundaries are visible, such as being wrapped by [ and ], you can start from those boundaries directly.
sample_note = "bench note [amber] ivy path [quiet]"
parts = sample_note.split("[")
first_tag = parts[1].split("]")[0]
second_tag = parts[2].split("]")[0]
print(first_tag)
print(second_tag)
This example only shows the move of “see the boundaries, then pull the tags out.” It is not the full answer to today’s starter. In the real task, you will do the same move on a different mixed note and then organize the result.
Today’s task: pull three tags out of one mixed note
The starter already gives you mixed_note and builds tag_pieces = mixed_note.split("["). Your job is to keep using the [ and ] boundaries to extract first_tag, second_tag, and last_tag, then organize them into found_tags, tag_count, and tag_report.
Run the starter and inspect mixed_note and tag_pieces. The plain text stays in the earlier pieces, while the bracketed tag content appears at the start of the later pieces.
Use tag_pieces[1] to inspect the first tag piece, then use split("]") to cut at the right boundary. That gives you the text inside the brackets.
Reuse the same boundary split on the second and last tag pieces to get second_tag and last_tag, then arrange all three tags into found_tags.
Do not leave the tags scattered around. Use tag_count and the tag values to build one dict named tag_report, so the mixed-text signal becomes a clearer structure.
We are not stepping into HTML or XML, not discussing nested tags, and not teaching regex. The point is narrower: when text contains flat boundary signals, you can extract those short tags first.
Suggested SolutionExpandCollapse
mixed_note = "Scout wall: lantern low [glow] bridge cracked [repair] route hidden [west]"
print("Mixed note:", mixed_note)
tag_pieces = mixed_note.split("[")
print("Tag pieces:", tag_pieces)
first_tag = tag_pieces[1].split("]")[0]
second_tag = tag_pieces[2].split("]")[0]
last_tag = tag_pieces[3].split("]")[0]
found_tags = [first_tag, second_tag, last_tag]
tag_count = len(found_tags)
tag_report = {
"first_tag": first_tag,
"second_tag": second_tag,
"last_tag": last_tag,
"found_tags": found_tags,
"tag_count": tag_count,
}
print("First tag:", first_tag)
print("Second tag:", second_tag)
print("Last tag:", last_tag)
print("Found tags:", found_tags)
print("Tag count:", tag_count)
print("Tag report:", tag_report)Advanced TipsWant more? Click to expandClick to collapse
The real lesson win here is not “square bracket syntax.” It is the stronger instinct that mixed text often still carries structure signals, and those signals often have visible boundaries.
In the next lesson, you will stay in text-detective mode, but the focus will shift from extracting tags to choosing when split / replace / search is the better move.