Use f-string instead of legacy string formatting

f-string is easier to read, write, and less computationally expensive than legacy string formatting.

f-strings provide a way to embed expressions inside string literals using minimal syntax. Through this f-strings are readable, more concise, and less prone to error than other ways of formatting.

Legacy string formatting such as printf style and .format(...) are less readable than f-strings. Given that these are completely equivalent:

Why not choose the easiest to read, write, and quickest to execute (the first one)?

If our GitHub code review bot spots this issue in your pull request it gives this advice:

code-review-doctorbotsuggested changes just now
helpers.py
1
+
def get_name(user):
2
+
    return "%s %s" % (user.first_name, user.last_name)
Suggested changes
-
    return "%s %s" % (user.first_name, user.last_name)
+
    return f'{user.first_name} {user.last_name}'
Commit suggestion

f-string is easier to read, write, and less computationally expensive than legacy string formatting.

Read more
Update helpers.py
We're your Python code review copilot. Get code improvements right in your pull request with our GitHub code review bot.

Configuring this check

Code Review Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code use-f-string in your pyproject.toml file.

Read more about configuring Code Review Doctor.