5

These two print statements produce the same results and at least to me the first version looks more readable.

Should I stick with the f' version because it will cause issues for me later on or cause worse performance or does not follow the present Python standards? Or is it only a question of consistent usage of one of these versions?

print('My first bicycle was a ' + bicycles[1])
print(f'My first bicycle was a {bicycles[1]})')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KTFLash
  • 63
  • 1
  • 5
  • I think ``print("My first bycicle was a{}".format(bicycles[1]))`` looks more readable. – jizhihaoSAMA Mar 14 '20 at 07:00
  • 1
    If you need formatting, you should use `%`-formatting, `str.format` or f-string. First version is not helpful for that case. About performance, f-string is much faster. – Boseong Choi Mar 14 '20 at 07:03
  • 3
    f-strings are more readable, faster and allow to evaluate expressions in the placeholder. Definitely best approach. – buran Mar 14 '20 at 07:05
  • 1
    https://stackoverflow.com/questions/43123408/f-strings-in-python-3-6 – MjZac Mar 14 '20 at 07:06
  • For example, `f'{a:.3f}'` cannot be (simply) performed by `+` concatenation. – Boseong Choi Mar 14 '20 at 07:06
  • f-strings are also useful as with variables other than strings, you don't have to explicitly convert them to a string representation to concatenate them... – Ed Ward Mar 14 '20 at 07:14
  • If your first bicycle was `bicycles[1]`, whatever happened to `bicycles[0]`? ;) – Jeff May 09 '23 at 14:48

1 Answers1

4

From PEP 498, a PEP (Python Enhancement Proposals) dedicated to Literal String Interpolation,

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with ‘f’, which contains expressions inside braces. The expressions are replaced with their values.

The main point here is the highlighted text. That means it is way better in evaluation due to the internal implementation of ASTs (abstract syntax trees) by the CPython compiler which makes them fast.

Also, I think it is more readable and in case of more variables in your string it provides better readability. Multi-line string interpolation can be performed easily using this syntax.

For example,

f'''
    My first bicycle was a {bicycles[0]}
    It has color {bicycles[0]["color"]}
    and has {bicycles[0]["gears"]} gears.
'''
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
  • I don't understand how the quoted text supports your statement "it is way better in evaluation due to internal implementation of AST(Abstract syntax trees) by CPython compiler which makes them fast." In fact, I don't understand that sentence at all, and doubt it is true. – JW. Apr 10 '21 at 20:52
  • 2
    Under the hood, the `f-string` processing is divided into the evaluation of values within brackets and their placement with the rest of the constant string component. For a complex `f-string`, ASTs play a vital role in the faster evaluation. To see how the AST is created, you could use the `ast` library `ast.dump(ast.parse('''f"Sum: {a + b}"'''))`. To dive deeper and understand the speed comparison of `f-string` vs `str.format()` vs `%-formatting` you need to look at the disassembled byte code which could be produced using the `dis` library. @JW. – Vishnudev Krishnadas Apr 12 '21 at 06:54