Code Logo

Shelf Tag Cleanup with startswith()

Published at22 Apr 2026
Python String Handling Easy 0 views
Like0

A stockroom printer has been adding a standard marker to some shelf tags before they are sent to the floor. Tags that come from that printer begin with "SKU-", while older tags and hand-written labels may not.

The cleanup rule is narrow and deliberate: remove "SKU-" only when it appears at the very beginning of the string. If those letters show up later in the tag, they are part of the real label and must stay exactly where they are.

That detail matters. A tag like "SKU-204" should become "204", and "SKU-BOOK-17" should become "BOOK-17". But a label such as "DELUXE-SKU-55" should stay unchanged, because the prefix is not at the front. The same goes for "204": there is nothing to remove, so the output is still "204".

This is a string-cleaning task with a very specific boundary. You are not replacing every occurrence of "SKU-" in the whole string. You are only checking whether the tag starts with that marker and removing it if it does.

Example Input & Output

Example 1
Input
tag = "DELUXE-SKU-55"
Output
"DELUXE-SKU-55"
Explanation

The text contains SKU later in the label, but not as a true prefix.

Example 2
Input
tag = "SKU-204"
Output
"204"
Explanation

The shelf marker is at the front, so it is removed.

Example 3
Input
tag = "SKU-BOOK-17"
Output
"BOOK-17"
Explanation

Only the leading marker is removed and the rest of the label stays intact.

Algorithm Flow

Recommendation Algorithm Flow for Shelf Tag Cleanup with startswith()
Recommendation Algorithm Flow for Shelf Tag Cleanup with startswith()

Solution Approach

The reliable way to solve this in Python is to test the front of the string explicitly with startswith(). That fits the actual rule of the stockroom: only remove the marker when it appears at the very beginning of the tag.

The temptation here is to use a broad replacement such as replace("SKU-", ""), but that would be too aggressive. A label like "DELUXE-SKU-55" would be damaged even though the embedded text is part of the real tag. The problem is really about checking one boundary, not cleaning every matching substring.

With startswith(), the intent stays precise. First ask whether the label begins with "SKU-". If it does, return the remainder of the string after those first four characters. If not, return the tag unchanged.

That leads to a small solution:

if tag.startswith("SKU-"):
    return tag[4:]
return tag

Each branch maps directly to the business rule. A front marker is removed. Anything else is left alone. That makes the edge cases easy to reason about as well. "SKU-" becomes an empty string, while "204" stays "204".

You could also solve the same task by combining the prefix check with a targeted one-time replacement, such as removing only the first occurrence after confirming that the string starts correctly. That still works because the important guard is the prefix test. The real lesson is that startswith() gives you the correct yes-or-no check before you modify anything.

So this challenge is really about choosing the method that matches the boundary condition. Once you know the cleanup should happen only at the front, startswith() becomes the natural Python tool for deciding whether the trim should happen at all.

Best Answers

python - Approach 1
def clean_shelf_tag(tag):
    if tag.startswith("SKU-"):
        return tag.replace("SKU-", "", 1)
    return tag