Python f-strings
f-stringsedit
Python f-strings are a powerful new way to format strings in Python. In general, they look like this:
f'This string includes {expression}'
Anything inside the curly-braces is evaluated as a Python expression. You can find full details by searching "Python f-strings" online. This is a good place to start.
f-strings in TouchDesigneredit
Some TouchDesigner operators allow you to use f-string formatting in various ways. Examples include textCOMP in the parameters and lister in the colDefine table. Different variables will be provided to the f-string for use in expressions. These will be documented in the individual operator pages.
Examplesedit
{text}!
- yields the string held intext
followed by an exclamation point{val + 1}
- yieldsval
+ 1 (works whenval
is a numeric type only)${val:.2f}
- yieldsval
as a float with two decimal places and a dollar sign before it (works whenval
is a float only){me.path}: {text}
- yields the operator path where this is being evaluated, followed by a colon, followed by the string held intext
Using f-string Formatting in Your Scriptsedit
To use f-string formatting in your custom scripts, parameters, and components, use the utility function formatString
in TDFunctions.