Skip to content

Simplify f-string Formatting

Sourcery refactoring id: simplify-fstring-formatting

Description

Simplify the formatting of replacements within an f-string.

Before

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

After

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

Explanation

This refactoring simplifies f-string:

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