r/learnpython • u/Typical-Road9611 • 16d ago
Are f-strings enough?
I've been learning python mainly from bro code's videos and he uses f strings for basically everything. It comes to me naturally to use them and I understand them. I found a book (Kenneth A. Lambert: Fundamentals of python: first programs, second edition) and here he uses other type of strings (I assume this would be the basic way) but I honestly don't understand them especially when it's about formatting (.2f etc). Should I learn this basic way of working with strings now? Or are f strings enough?
I should add that I'm learning python for fun, I'm studying mechanical engineering, so I know it can be very useful. I also want to be good at it in a potential work environment (?)
View all comments
Show parent comment
1
u/markhahn 15d ago
I'd argue that this is nearly trivial. yes, if the logger is not logging, you save on doing the formatting. but args to that logging call are still evaluated before the call, so still cost you. I guess it's mainly a question of whether the actual formatting+interpolation is numerous/frequent enough for you to care. being lazy about converting an int to text once a second is utterly trivial, but inside a loop a billion times a second, yeah.
then again, if you're doing that many calls to the logger (lazy or eager), you're probably already in trouble...
I guess a pertinent question would be: how often does real code introduce very expensive str or repr functions? one could make these arbitrarily expensive, but does that happen?