3 ms·
F-strings are the one obvious way to do string formatting. There may be other ways, for legacy backwards compatibility reasons, but f-strings are the way to do
by orf 7y ago
F-strings are the one obvious way to do string formatting. There may be other ways, for legacy backwards compatibility reasons, but f-strings are the way to do string formatting.
- kzrdude 7y agoI think you are right. Just left a string of legacy (%-formatting) and dead end (.format()) solutions on the way there.
- cochne 7y agoI disagree. In logging for example, ‘%s’ with the value as an argument to the logger is preferred because the formatting can be ignored if the log level is not sufficient to print.
- orf 7y agoLog formatting is a different problem than string formatting. With log formatting you pass the formatting arguments as function parameters, which is completely different from any other way you format strings.
- AlexandrB 7y agoI think you're getting downvoted because logging is basically doing: def log(fmt, *args): print(fmt % args) Hardly a huge change.
- heavenlyblue 7y agoExcept log formatting isn’t the same as actually executing the expression passed to the logging function.
- mixmastamyk 7y agoYes, unless a template is needed later. That's why the others continue to exist.
- AlexandrB 7y agoI think F-strings are bad for i18n. It sucks to use them with a database of string localizations because the variable name is now embedded in dozens of translated F-strings and basically becomes immutable.