ComfyUI Node

removeprefix

Removeprefix — the polite way to delete the front of a string

By StableLlama·Created about a year ago·Updated 4 days ago· 48
removeprefix
    • STRING
    string
    prefix

    Stripping a prefix is one of those jobs with a wrong way to do it. The wrong way is a brute-force remove that deletes the characters whether or not the prefix is actually there, mangling strings that don't match. The right way is removeprefix: it removes the prefix only if the string actually starts with it, and otherwise returns the string untouched. No errors, no partial strips, no collateral damage.

    Reach for it when you need to clean the front of strings: stripping img_ off a batch of filenames, removing https:// before you parse a URL, pulling a folder name out of a path, or deleting a marker token from the start of a prompt. Because it's safe when the prefix is absent, you can chain it without guarding every call.

    How it works

    Python's str.removeprefix(prefix), which is exact and literal: the prefix must match character-for-character at the very start of the string, case included.

    • "image_001.png".removeprefix("image_")"001.png"
    • "plain.png".removeprefix("image_")"plain.png" (no match, unchanged)
    • "Photo.png".removeprefix("photo")"Photo.png" (case-sensitive, no match)
    • "sub_image.png".removeprefix("image")"sub_image.png" (prefix must be at the start, not anywhere)

    That "safe when absent" property is the whole appeal. A plain slice or character-strip would silently eat the wrong thing; this never does.

    Inputs and outputs

    Two required inputs:

    • string - the text to clean.
    • prefix - the exact string to remove from the front.

    Output is one STRING - the cleaned copy.

    Installing it

    Ships in the Basic data handling pack by StableLlama. ComfyUI Manager, search "Basic data handling", install, restart. Manual route:

    cd ComfyUI/custom_nodes
    git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
    

    Pure Python, zero dependencies, no downloads. Under Basic/STRING in the menu.

    Common issues

    Case-sensitivity is the quiet trap: removeprefix("Photo", "photo") leaves the string alone because the P doesn't match p. If your data has unpredictable case, run it through this pack's lower node first and match on the normalized value. And don't confuse this with lstrip: lstrip removes sets of characters anywhere they appear at the front, while removeprefix removes one exact literal prefix. lstrip("ab") on "aaabx" gives "x"; removeprefix("ab") on "aaabx" gives "aaabx" unchanged because the string doesn't start with ab. Different tools, different jobs.

    CategoryBasic/STRING

    Inputs (2)

    NameTypeDefaultDescription
    stringSTRING
    prefixSTRING

    Outputs (1)

    NameTypeDescription
    STRINGSTRING