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

28 Upvotes

44 comments sorted by

View all comments

7

u/seanv507 19d ago

I would say f strings are enough. The only place where i have seen fstrings discouraged (linter hints) is in logging. Thr argument being that even if logging is turned off, any complex calculation only used for outputting a log message still gets used. In any case, yes only use f strings, and definitely start using logging. https://pylint.readthedocs.io/en/stable/user_guide/messages/warning/logging-fstring-interpolation.html

2

u/Typical-Road9611 19d ago

Thank you. I will look up what logging is, I've only been learning a few days. I can write basic lil programs (half of bro code's 12 hour video)

1

u/ba-na-na- 19d ago

The point is that f-strings render (“interpolate”) the string before passing it to the function. While the other approach passes the formatting and the arguments, but rendering happens inside the function. So if the function is ignoring the inputs (like when logging is disabled), you don’t pay the penalty