19

One of the features of Python 3.6 are formatted strings.

This SO question(String with 'f' prefix in python-3.6) is asking about the internals of formatted string literals, but I don't understand the exact use case of formatted string literals. In which situations should I use this feature? Isn't explicit better than implicit?

Community
  • 1
  • 1
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
  • For example generating queries for query languages (should use prepared queries where available) or you want a single string that has variable parameters like a list or formatted float etc. – Fma Aug 22 '16 at 14:16
  • I think this is somewhat broad and/or opinion-based, but I see three benefits: 1) it's shorter; 2) impossible to mess up the order of parameters in `format` (you can use named parameters, but then it's _much_ shorter); 3) immediately clear which variable goes where without jumping back and forth between format string and parameter list. – tobias_k Aug 22 '16 at 14:17
  • 5
    Beware of f-strings! They are _extremely_ addictive. After a few days of using them it's hard to go back to other kinds of formatting. :) – PM 2Ring Aug 22 '16 at 14:51
  • Possible duplicate of [String with 'f' prefix in python-3.6](http://stackoverflow.com/questions/35745050/string-with-f-prefix-in-python-3-6) – Chris_Rands Sep 30 '16 at 12:19
  • Docs: https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals – handle Dec 05 '19 at 08:55

3 Answers3

35

Simple is better than complex.

So here we have formatted string. It gives the simplicity to the string formatting, while keeping the code explicit (comprared to other string formatting mechanisms).

title = 'Mr.'
name = 'Tom'
count = 3

# This is explicit but complex
print('Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count))

# This is simple but implicit
print('Hello %s %s! You have %d messages.' % (title, name, count))

# This is both explicit and simple. PERFECT!
print(f'Hello {title} {name}! You have {count} messages.')

It is designed to replace str.format for simple string formatting.

Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
xmcp
  • 3,347
  • 2
  • 23
  • 36
  • While I agree, one could argue that the former _could_ be written much shorter as `'Hello {} {}! You have {} messages.'.format(title, name, msg_count)` – tobias_k Aug 22 '16 at 14:29
  • 3
    well, it's very "implicit" when you have a dozen of `{}`s in the string – xmcp Aug 22 '16 at 14:30
  • Agreed again, but that means that it's not just brevity. Its brevity while retaining the explicitness of having the actual variable names in the format string. (On second thought, I think that's exactly what you were trying to say... +1) – tobias_k Aug 22 '16 at 14:31
3

Pro: F-literal has better performance.(See below)

Con: F-literal is a new 3.6 feature.

In [1]: title = 'Mr.'
   ...: name = 'Tom'
   ...: count = 3
   ...: 
   ...: 

In [2]: %timeit 'Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count)
330 ns ± 1.08 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [3]: %timeit 'Hello %s %s! You have %d messages.'%(title, name, count)
417 ns ± 1.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [4]: %timeit f'Hello {title} {name}! You have {count} messages.'
13 ns ± 0.0163 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
zhengcao
  • 451
  • 5
  • 4
1

Comparing Performance for all 4 ways of string formatting

title = 'Mr.' name = 'Tom' count = 3

%timeit 'Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count)

198 ns ± 7.88 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit 'Hello {} {}! You have {} messages.'.format(title, name, count)

329 ns ± 7.04 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit 'Hello %s %s! You have %d messages.'%(title, name, count)

264 ns ± 6.95 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit f'Hello {title} {name}! You have {count} messages.'

12.1 ns ± 0.0936 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

So the latest way of string formatting is from python3.6 is fastest also.

Harshit
  • 207
  • 1
  • 5