f"{variable=}"
Standard Library Idioms

Meaning

It produces an f-string that prints the source expression text followed by an equals sign and the repr of its value. This saves the developer from manually typing the expression twice, reducing repetitive code and typo risk. It is used when quickly inspecting expression values during debugging or interactive sessions.

Primary Function

Debugging

Communicative Purpose

Avoids repetitive typing of expressions in debug output by automatically including the expression text and its value

Pattern

f"{expr=}"

Core Structure

f"{...=}"

Função primária

Debugging

Propósito comunicativo

Avoids repetitive typing of expressions in debug output by automatically including the expression text and its value

Situações de gatilho

Data analysis notebooks: inspecting intermediate variables during exploratory analysis; Web application development: debugging request handlers by printing request parameters; Command-line scripts: quick sanity checks of computed results

Contextos

Python 3.8+, debugging workflows, logging, data science notebooks, quick script inspection

Padrão

f"{expr=}"

Estrutura central

f"{...=}"

Slots de substituição

variable_or_expression: any valid Python identifier or expression (e.g., x, x+1, func(), obj.attr)

Colocados típicos

  • print()
  • logging.debug()
  • breakpoint()
  • f-string formatting specifiers (!r
  • !s
  • :.2f)

Substituições comuns

  • Expressions: f"{x+1=}"
  • f"{func()=}"
  • f"{obj.attr=}"
  • Format specifiers: f"{x=!r}"
  • f"{x=:>10}"

Erros comuns

Using in production logging instead of structured logging; forgetting it requires Python 3.8+; using with complex expressions that reduce readability

Similar / contraste

f"{var}" prints only value; f"{var!r}" prints repr(value); logging.debug('var=%s', var) for structured logging

Interferências

Coming from C/Go/Java: expecting printf-style format to auto-include variable names → use f-string {var=} syntax; Coming from JavaScript: expecting console.log({var}) to produce similar output → use f'{var=}' for similar output

Família do chunk

  • f-string formatting
  • debug printing
  • print debugging
  • logging patterns

Nuance

Only available in Python 3.8+. The = specifier prints the source expression text followed by = and the repr of the result. Not suitable for production structured logging — use logging module with formatters instead.

Efeito pragmático

Eliminates repetitive 'var=' + str(var) concatenation; makes debug output self-documenting; reduces typo bugs in debug prints

Dica de memória

Debug print with equals sign: the variable name appears automatically before the value

Nota

Introduced in Python 3.8 via PEP 572 (assignment expressions) though the f-string = specifier is a separate feature. Works with any expression, not just variable names.

Upgrade path

Structured logging: logging.debug('user_count=%d', user_count) or structlog with key-value pairs

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: f-string debug specifierPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Short-term

Log in to save chunks.