Nodes/Comfy Text Stats/Text -> String Truncate
ComfyUI Node

Text -> String Truncate

Cut a string down to N characters or words — from either end

By scofano·Created 11 months ago·Updated 3 months ago· 3
Text -> String Truncate
    • text
    text
    truncate_to10
    truncate_bycharacters
    truncate_fromend

    Need to cut a string down to size - a hard cap on prompt length, an over-long filename, a preview snippet - this node does it by character or by word, from either end. It's one of those deceptively small nodes in the Comfy Text Stats pack where a single confusing input (truncate_to, and its negative numbers) does a lot of work. Pure Python, part of the same scofano pack, nothing extra to install.

    The controls

    Four inputs:

    • text - the string being truncated.
    • truncate_to - the target length. Positive: keep that many units from the chosen side. Negative: Python-style slicing from the chosen side, i.e. remove that many.
    • truncate_by - characters or words.
    • truncate_from - end or beginning.

    The negative values are where beginners get lost, so here's the cheat sheet for truncate_to:

    • end, positive → keep the last N (e.g. the last 10 chars).
    • end, negative → drop the last N, keep everything before.
    • beginning, positive → keep the first N.
    • beginning, negative → drop the first N, keep the tail.

    Word mode splits on whitespace and does the same thing word-by-word. Zero length or empty input gives you an empty string.

    Why you'd reach for it

    Prompt engineering is the obvious one: if a downstream node or an API has a length cap, truncate before it gets there. Metadata and filenames are the sneaky second use - a generated filename built from a long caption stops being usable around a hundred characters. And if you're previewing text in a workflow, truncating to a fixed word count gives you tidy, predictable snippets.

    Installing it

    It's in the Comfy Text Stats pack, so you get all eight nodes in one go. ComfyUI Manager: Manager → Custom Nodes Manager → search "Comfy Text Stats" → Install, then restart ComfyUI. Or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/scofano/comfy-text-stats
    

    Restart after. No dependencies, no models to download - the pack's requirements file is literally a comment saying there are none.

    Where people get burned

    • The negative-number semantics are Python's, not "remove from the front." Test on a sample string before trusting it in a pipeline.
    • Character truncation can split mid-word. If you need clean cuts, set truncate_by to words.
    • Truncating "from the end" keeps the tail of the text, which reads backwards for a human. If you want a preview that starts at the beginning of the string, set truncate_from to beginning - the default is end, and people trip on that constantly.
    CategoryText/Utils

    Inputs (4)

    NameTypeDefaultDescription
    textSTRING
    truncate_toINT10-999999–999999
    truncate_byCOMBOcharacters2 options: characters, words
    truncate_fromCOMBOend2 options: end, beginning

    Outputs (1)

    NameTypeDescription
    textSTRING