Row descriptions
Create readable labels by combining several columns in a projection.
Polars · Function
Creates a Polars string expression by filling a template with literal values or expression results. It supports sequential, indexed, and column-name placeholders.
pl.format creates a string expression from a template and replacement expressions. Placeholders can consume arguments by position or refer directly to column names, and the resulting expression can be used in contexts such as select or with_columns.
polars.format(*f_string: str, **args: IntoExpr) → Expr| Name | Type | Description |
|---|---|---|
f_string | str | Template string containing {}, indexed, or named placeholders. |
args | IntoExpr | Expressions that supply values for the template placeholders. |
Expr
{} placeholders to consume supplied arguments from left to right.{{ and }}.import polars as pl
df = pl.DataFrame({"name": ["Ada", "Linus"], "age": [36, 55]})
result = df.select(
pl.format("{} is {} years old", pl.col("name"), pl.col("age")).alias("description")
)
print(result)Builds one formatted string per row from two column expressions.
Create readable labels by combining several columns in a projection.
Construct standardized identifiers from columns and fixed text.
Embed column values in report-friendly messages.
pl.format return, and what are its main inputs?pl.format returns a Polars Expr that formats expression values as strings. Its first argument is the template, and subsequent keyword or positional arguments supply placeholder values.
Follow-up: How can a template address arguments without relying on their order? Use indexed placeholders such as {0}, or named placeholders that refer directly to column names.
pl.format support?An empty {} consumes the next argument, while {index} selects the argument at that position. A placeholder containing an ASCII identifier refers directly to a column name.
Follow-up: What happens when a referenced input is null? The result is null for that row, rather than a partially formatted string.
pl.format template?The template must escape literal braces by doubling them. For example, a JSON-like literal can use {{"json": 42}} in the format string.
Follow-up: How would you preserve row-wise behavior? Keep the inputs as Polars expressions so each row is evaluated independently.
polars.Exprpolars.colpolars.DataFrame.selectpolars.DataFrame.with_columns