2

Which one is better in python3? They have same output but most of codes are using format instead F string.

a = "Test" print(f"this is for {a}")

or format?

print("This is for {}".format(a))

Some times when I used F string for Directory and file path I got some errors but there were no problem with using format.

  • 4
    Better might be subjective, but the realpython website has a nice arcticle on why you should use one or the other. https://realpython.com/python-f-strings/ . They do favor the more modern version, so F string. – JustLudo Apr 22 '20 at 15:19
  • 1
    Using f with path has the enclosed '' "" problem. You don't have that with format. Using f has usually shorter code. It's your personal preference what to use when. – Mace Apr 22 '20 at 15:22
  • 1
    `format` would remain the only option in certain dynamic settings (consider that `"This is for {}".format` is a bound method like any other) and for backwards compatibility, but all things being equal, prefer an f-string. The need to support Python 3.5 and earlier should decrease over time. – chepner Apr 22 '20 at 15:24
  • You can define a template string somewhere, say in a message.py file and then use the template elsewhere. Or you can pass it as a parameter to a method. An f-string is used where it is defined. Plus, as chepner said, you can't dynamically build an f-string, you have to write it as a literal. For most cases, f-string is probably a bit easier and more Pythonic. The RealPython was very rah-rah about f-strings IIRC but that position is only really justified when you have the pick of both. f-strings are not always available in all contexts. – JL Peyret Apr 22 '20 at 16:00
  • Does this answer your question? [String formatting: % vs. .format vs. f-string literal](https://stackoverflow.com/questions/5082452/string-formatting-vs-format-vs-f-string-literal) – Gino Mempin May 01 '23 at 15:04

1 Answers1

0

As pointed out in the comments, a good comparison of string formatting methods is provided on the Real Python website.

f-strings in many situations can be more readable and less human-error prone to code than other variants, but require Python >= 3.6, so may have to be avoided if backwards compatibility is required. In general they are a good choice though, up to a few gotchas that come up from time to time.

When nesting f-strings you have to be careful with quotation marks. This fails:

>>> f"Hello {"there"}"
  File "<stdin>", line 1
    f"Hello {"there"}"
                  ^
SyntaxError: invalid syntax

But using other quotation marks on the inside lets you get around this:

>>> f"Hello {'there'}"
'Hello there'

You cannot nest f-strings containing string literals deeper than that though, because you have no further different quotation marks to use.

Another gotcha I stumble across regularly is the restriction on not allowing backslashes in the f-string expression part, even if they are inside a string literal:

>>> f"Path: {'C:\Windows'}"
  File "<stdin>", line 1
SyntaxError: f-string expression part cannot include a backslash

You can get around that using an intermediate variable, or format():

>>> path = 'C:\Windows'
>>> f"Path: {path}"
'Path: C:\\Windows'

>>> "Path: {0}".format('C:\Windows')
'Path: C:\\Windows'

This is probably the issue you had with using f-strings to format paths. I personally tend to encounter this limitation when working with string literals that have newlines '\n' in them inside the f-string expression part.

pallgeuer
  • 1,216
  • 1
  • 7
  • 17