🐸

The Name Formatter

The Data Tinkererpython-data-tinkerer-07-the-name-formatter
Reward: 100 XP
|

The Name Formatter

After passing through the crystal gate, Hoppy reaches the nameplate workshop. Every travel badge here needs to be cleaned, checked, and polished before it can hang on an adventure pack. Earlier, you brushed text edges, replaced a tiny rune, and checked edge signals.

Now one rough badge is waiting on the table: " badge: hoppy-hopper ". This is not a new-spell lesson. It is a light checkpoint: can you chain a few familiar text moves and make a name that is ready to display?

First, see what this tiny action chain gives you

The key experience here is not learning a new method name. It is connecting a few small moves in order: clean the edges, inspect the starting signal, then reshape the text into something you would actually want to display.

raw_label = "  badge: moss-runner  "

print("Before:", "[" + raw_label + "]")

clean_label = raw_label.strip()
has_badge_prefix = clean_label.startswith("badge:")
display_label = clean_label.replace("badge: ", "").replace("-", " ")

print("Starts with badge:", has_badge_prefix)
print("Display:", "[" + display_label + "]")

When you run it, three things show up together: the original nameplate still has padded edges, the prefix check becomes True, and the final display name becomes hoppy hopper. That is the mini-review for this part of the chapter: edges can be cleaned, beginnings can be checked, and one awkward piece in the middle can be repaired.

Turn familiar moves into one light combination

You do not need a new core idea here. You are just connecting moves you already know: strip() brushes off extra whitespace at the edges, startswith() checks whether the cleaned text begins with the signal you care about, and replace() turns the noisy inside pieces into something better for display.

Later, when a piece of text is almost usable but not fully tidy yet, try asking: should I clean the edges first? Should I check the opening signal? Should I fix one last small piece before showing it? For now, this lesson is just about getting comfortable with that light chain.

1
Find the three placeholder lines

The starter already shows the raw nameplate and the output lines. The real work is on the three lines for clean_nameplate, has_badge_prefix, and display_name.

2
Clean the edges, then check the start

First use raw_nameplate.strip() to get a cleaner version. Then use startswith("badge:") on that cleaned string for a quick prefix check.

3
Turn the nameplate into a display name

Use replace() to remove the badge: prefix, then change the hyphen - into a space. Run it and compare before / display so you can see that the rough nameplate really became something ready to show.

Why keep the True line too?

Because this checkpoint is not only about getting the final display name. It is also about confirming that you can still make one small judgment on the way: does this cleaned string actually begin with badge:?

Suggested Solution
Expand
Solution:
raw_nameplate = "  badge: hoppy-hopper  "

print("Before:", "[" + raw_nameplate + "]")

clean_nameplate = raw_nameplate.strip()
has_badge_prefix = clean_nameplate.startswith("badge:")
display_name = clean_nameplate.replace("badge: ", "").replace("-", " ")

print("Starts with badge:", has_badge_prefix)
print("Display:", "[" + display_name + "]")
Advanced Tips
Want more? Click to expand

Programmers might call this a tiny formatting pass: turning raw text into a version that is easier to show. You do not need any new formatting syntax for that yet. The small chain of actions matters more than the label.

In the first part of this chapter, you cleaned edges, swapped out small fragments, and checked edge signals. Now add one more judgment to your toolkit: when text is almost ready, a few light string moves in a row can be enough to create a much cleaner final output.

Loading...
Terminal
Terminal
Ready to run...