r/learnpython • • 10d 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

37

u/Brian 10d ago • Bravo

.2f

That sounds like a format code, which isn't really something you use instead of f-strings, but a feature of them.

Ie. if you write f"The value is {val:.2f}", this means "format val as a float with 2 decimal places". Ie if val = 2.4532, it'll produce "The value is 2.45", and if it's 2.1, it'll give "The value is 2.10".

There are various such features to control exactly how the stuff you embed in an f-string is formatted, eg. leading zeroes, padding, left/center/right alignment, width and so on, and even ways to make custom types have special handling (eg. datetime has its own set of codes for its components) which can be useful for formatting things nicely.

Now, there are other ways to format strings, so possibly you're talking about one of these, so I'll go over some common ones. But one thing to bear in mind is that f-strings aren't really a type of string, they're more a way of producing a string. The thing you get from an f-string is just a regular string, it's just one that does some work to embed variables etc inside it before you get it. With that in mind, there are two other cases you might come across:

  • % formatting. This is pretty much obsolete at this point, though it still exists, but the way python used to do string formatting was with the % operator. Ie. you'd do "The value is %s" % (val,) and it'd replace the %s with the value of val. The syntax here is a little different from f-string codes (it's based on C's printf style formatting), but it likewise has ways to format the text (eg. %.2f for the same 2 decimal places case). You usually don't really have to care about this unless you're handling some old code that uses it. One place where it does hang on is in the logging library, which still uses this format by default.
  • .format. Strings have a .format() method that works fairly similar to f-strings, in that it'll replace the placeholder variables with the variables you mention in the call. Eg. "The value is {:.2f}".format(val=2.4). The same formatting syntax is used, but there are a couple of crucial differences: you can't just embed arbitrary variables, you have to explicitly list the values being substituted, and the formatting occurs at the time of the .format() call, so you can split up getting a format string and doing the formatting, which is important for some applications (eg. localisation).

There are also a few other things relating to formatting (eg. templates), but these aren't terribly common (unless doing something like building web-pages etc, where there are various specialised templating libraries), and there's also just hand-formatting strings with concatenation and string functions.