Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Function

pl.format

Creates a Polars string expression by filling a template with literal values or expression results. It supports sequential, indexed, and column-name placeholders.

How it works

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.

Syntax

polars.format(*f_string: str, **args: IntoExpr) → Expr

Parameters

NameTypeDescription
f_stringstrTemplate string containing {}, indexed, or named placeholders.
argsIntoExprExpressions that supply values for the template placeholders.

Returns

Expr

Key points

  • The function builds a string-valued Polars expression from a template.
  • Use {} placeholders to consume supplied arguments from left to right.
  • Indexed placeholders let the template select a specific argument by position.
  • ASCII identifier placeholders can refer directly to columns by name.
  • Replacement values are expressions, allowing formatting to be evaluated per row.

Common mistakes

  • Literal braces must be doubled in the format string, such as {{ and }}.
  • A null in any input expression produces a null formatted value for that row.

Example: Format row values

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.

When to use it

Row descriptions

Create readable labels by combining several columns in a projection.

Composite keys

Construct standardized identifiers from columns and fixed text.

Display strings

Embed column values in report-friendly messages.

Interview questions

QWhat does 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.

QWhat placeholder forms does 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.

QHow are literal braces represented in a 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.Expr
  • polars.col
  • polars.DataFrame.select
  • polars.DataFrame.with_columns

Source: polars.format — Polars documentation - Polars user guide. Checked 2026-10-15.