logoalt Hacker News

aunderscoredyesterday at 9:23 PM1 replyview on HN

If you're interested, fstrings are faster because they directly become bytecode at compile time rather than being a function call at runtime


Replies

apelapanyesterday at 10:47 PM

Thanks for the that bit of info! I was surprised by the speed difference. I have always assumed that most variations of basic string formatting would compile to the same bytecode.

I usually prefer classic %-formatting for readability when the arguments are longer and f-strings when the arguments are shorter. Knowing there is a material performance difference at scale, might shift the balance in favour of f-strings for some situations.