Skip to content

Simplify f-string Formatting

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: simplify-fstring-formatting

Section titled “Sourcery refactoring id: simplify-fstring-formatting”

Simplify the formatting of replacements within an f-string.

name = "Tom"
celebration = "hurrah"
f"{name} is {5} years old, {f'{celebration.upper()}'}"
name = "Tom"
celebration = "hurrah"
f"{name} is 5 years old, {celebration.upper()}"

This refactoring simplifies f-string:

  • Inline constants directly into the f-strings
  • Remove unnecessary nested f-strings if they are not needed