Skip to content

Use Fstring For Concatenation

Sourcery refactoring id: use-fstring-for-concatenation

Description:

Use f-strings for concatenating strings instead of '+'

Before:

output = "Name: " + name + ", Age: " + age

After:

output = f"Name: {name}, Age: {age}"

Explanation:

Python added f-strings in version 3.6, with PEP 498. F-strings are a flexible and powerful way to concatenate strings. They make the code shorter and more readable, since the code now looks more like the output.

This refactoring replaces string concatenations using '+' with f-strings. This will only trigger if the concatenation is a mixture of strings and calculated values.

Back to top