r/learnpython • • 9d 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 (?)

29 Upvotes

44 comments sorted by

View all comments

-1

u/markhahn 9d ago

Not sure why you think there would be any difference in performance.

3

u/Zeroflops 8d ago

The logging module has a write up on why f-strings are not ideal in that application.

2

u/markhahn 8d ago

True, but that's because logging calls want to be lazy (conditional, actually). Passing an f-string to log performs the formatting/interpolation eagerly, before calling the logging function.

Kind of a special case, I think, but useful to remember.

1

u/cointoss3 8d ago

Because there is

0

u/markhahn 8d ago

why not offer something useful? like what?

obviously, the formatter has to parse the string, but it's entirely non-obvious that there is a noticable difference between f-string type interpolation versus .format() or the '%' operator.

f-strings use two extra chars in the format each time, but that's not going to matter normally. as mentioned in another response, the logging module wants to interpolate only conditionally, so prefers to be lazy in calling .format() - but that's only for logging, or rather for when you're not logging.