> "Do one thing and do it well." _— Peter H. Salus / Doug McIlroy, [core Unix principle](https://en.wikipedia.org/wiki/Unix_philosophy)_ > "Simple is better than complex." _— Zen of Python_ # `String Constructor` (Text-Formatting) nodes ... for ComfyUI. [🔄 Updates ChangeLog](CHANGELOG.md) ### TL;DR There's already a plenty of string-formatting nodes for ComfyUI. But this node pack takes a different approach: - Build your dictionary of available text chunks once. Pass it further as a single line (bus/pipe design). - Easily reuse these sub-strings to build many variations of a prompt as needed. - It's especially handy for regional prompting (aka area composition). - [❗ Recursive formatting support](#recursive-formatting-) - ... - PROFIT!  ## The main `String Formatter` node It expects one input with a full "library" (dictionary) of all the named strings you might need, and it lets you use them in a prompt - any of them (or none at all), in any order, anywhere inside text template - using [python's string formatting syntax](https://docs.python.org/3/library/string.html#format-examples)... **Don't panic!** All the unfamiliar programming terms are explained below. But the main point is, you don't need to fiddle around connecting a bunch of wires every time you'd like to build another prompt, compiled from the same parts. There's just a single node, single input _(with all the available text pieces contained within it)_, single text field - and that's it. > [!NOTE] > Unlike many other _(giant uber-mega)_ node packs, this one strictly adheres to the modular philosophy: it's minimal and self-sufficient. It's designed to only do string formatting and nothing else. ### Dictionary For those unfamiliar, dictionary is a special datatype, which can contain a bunch of individual values (as many as you need), each of which accessible with its own **unique** name _(key)_. For our purposes, it can be understood as a simple "search & replace" table. So, you prepare a bunch of distinct parts of the prompt, you name them and then you can reference any of them individually. For example, in the screenshot shown above, this is the dictionary we get in the end (as the `Preview` node confirms): | Chunk Name (key) | Chunk Text (value) | |------------------|--------------------------------------------| | `model_prefix` |
score_9, score_8_up, score_7_up| | `char1_short` |
1boy, blond, short hair| | `char1_long` |
1boy, smiling, blue eyes, blond, short hair,[NEW LINE HERE]| And then, we can do **this** with the same input dictionary:  > [!NOTE] > The `BREAK` keyword shown here isn't natively supported by Comfy's `CLIP Text Encode`. But you can use [`CLIPTextEncode with BREAK syntax`](https://github.com/dfl/comfyui-clip-with-break) or [`CLIP Text Encode++` from smZNodes](https://github.com/shiimizu/ComfyUI_smZNodes) (if you want to fully switch from Comfy's to A1111's way of encoding text). ### String-Formatting syntax The way you "paste" values from the dictionary into your text template is called "string formatting". Its syntax is very simple: - You just put your `{key_name}` inside curly braces (no spaces between) - and voila! When the prompt template gets formatted, this pattern will be replaced with the actual sub-string. - For your convenience, the string-formatting node itself can show the actual output. - Also keep in mind, that while naming your sub-strings, you must follow the same restrictions python imposes on any variable names: only ASCII letters, digits and underscore are allowed + the name can't start with a digit. So: - ✅ `valid_name`, `_other_valid_name_`, `YetAnother_ValidName___`, `name4`. - ❌ `wrong name with spaces`, `wrong-name.with:punctuation`, `4name`. - Don't worry, the node pack will warn you if you type an invalid name. > [!NOTE] > For advanced users: > Internally, just a built-in `str.format()` is called with keyword arguments from the passed Format-Dict, which is literally just a dict with string keys. > > So any formatting patterns are available (like `{float_value:.3f}`) + `Add ANY to Format-Dict` node is there for exactly that. ### Recursive formatting
wearing a leather jacket, sitting on a bike