f-strings are fastest and most readable; format and % remain useful for dynamic templates
Percent formatting is the oldest style and is still common in logging. str.format() is more powerful and supports positional and keyword fields, but it is more verbose. f-strings evaluate expressions at runtime, are compiled into efficient bytecode, and are usually the fastest and most readable for local formatting. The main trade-off is that f-strings are eager: the expression is evaluated immediately, which matters for logging. For dynamic format strings from config or user input, str.format() or a templating engine is often safer.
Use f-strings for local, fixed format strings and debugging.
Use str.format() when the format template is dynamic or you need positional/keyword substitution.
Use %-formatting mainly for compatibility or logging's lazy formatting style.
Trade-off: f-strings are eager; logging with f-strings can waste work if the log level is disabled.
Common mistake: using f-strings for user-controlled templates, which can be an injection risk if combined with eval-like behavior.
Version note: f-strings landed in 3.6; = debug specifier in 3.8; nested quotes improved in 3.12.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience