8

With str.format() I can use tuples for accesing arguments:

>>> '{0}, {1}, {2}'.format('a', 'b', 'c')

'a, b, c'

or

>>> t = ('a', 'b', 'c')
>>> '{0}, {1}, {2}'.format(*t)

'a, b, c'

But with the new formatted string literals prefixed with 'f' (f-strings), how can I use tuples?

f'{0}, {1}, {2}'.(*t)  # doesn't work
smci
  • 32,567
  • 20
  • 113
  • 146
Trimax
  • 2,413
  • 7
  • 35
  • 59
  • 1
    What you mean by *But with the new formatted string literals prefixed with 'f' how can use tuples*? Also in your second sample you need to unpack the tuple. – Mazdak Aug 04 '16 at 09:53
  • Usage of f-strings depends a lot on the particular use case. Give us a realistic example on what you're trying to do, since the current problem would be best done with `', '.join(t)` in both 3.6 and pre-3.6 – Markus Meskanen Aug 04 '16 at 09:55
  • 1
    `'...'.format('a', 'b', 'c')` is a **method call**, one you called with 3 arguments. You did not pass in a tuple in your first example. In your second example, you are using the `*args` call syntax; `str.format()` still receives 3 separate arguments and doesn't know you used a tuple to supply those. – Martijn Pieters Aug 04 '16 at 09:55
  • @Kasramvd I mean that `f'{0}, {1}, {2}'.(*t)` doesn't works – Trimax Aug 04 '16 at 09:57

1 Answers1

11

Your first str.format() call is a regular method call with 3 arguments, there is no tuple involved there. Your second call uses the * splat call syntax; the str.format() call receives 3 separate individual arguments, it doesn't care that those came from a tuple.

Formatting strings with f don't use a method call, so you can't use either technique. Each slot in a f'..' formatting string is instead executed as a regular Python expression.

You'll have to extract your values from the tuple directly:

f'{t[0]}, {t[1]}, {t[2]}'

or first expand your tuple into new local variables:

a, b, c = t
f'{a}, {b}, {c}'

or simply continue to use str.format(). You don't have to use an f'..' formatting string, this is a new, additional feature to the language, not a replacement for str.format().

From PEP 498 -- Literal String Interpolation:

This PEP does not propose to remove or deprecate any of the existing string formatting mechanisms.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    `f'Values: {str(t)[1:-2]}'` ftw. No but seriously, new ways might end up popping from nowhere, based on the use case. I don't think this question can be answered too generally – Markus Meskanen Aug 04 '16 at 10:02
  • 1
    `str.format()` will not be deprecated in Python 3.6? – Trimax Aug 04 '16 at 10:02
  • No, I'm pretty certain it'll never go away during Python 3. @Trimax – Markus Meskanen Aug 04 '16 at 10:03
  • 1
    @Trimax: no, not at all. formatting strings are an **additional** feature, not a replacement. – Martijn Pieters Aug 04 '16 at 10:03
  • 3
    @MarkusMeskanen: That'll still include the quotes. Why not `', '.join(t)`? – Martijn Pieters Aug 04 '16 at 10:04
  • Indeed, that's what I suggested under comments for the original question too! @MartijnPieters The point I was trying to make is that there might not be one-to-one way of converting `.format()` to f-strings, but completely different ways will be discovered... And sometimes `.format()` is still the best option. – Markus Meskanen Aug 04 '16 at 10:08