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:
1 | + | def get_name(user): | |
2 | + | return "%s %s" % (user.first_name, user.last_name) |
f-string is easier to read, write, and less computationally expensive than legacy string formatting.
Read more- | return "%s %s" % (user.first_name, user.last_name) |
+ | return f'{user.first_name} {user.last_name}' |