0

print can nicely unpack a tuple removing brackets and commas, e.g.

a = (0, -1, 1) print(*a) 

produces 0 -1 1. Trying the same with f-strings fails:

print(f'{*a}') 

The closest f-string option I found is:

print(f'{*a,}'.replace(',', '').replace('(', '').replace(')', '')) 

Is there a more direct way to get 0 -1 1 from *a using f-strings?

7
  • 1
    Is f"{" ".join(map(str, a))}" what you look for? Commented Nov 6, 2024 at 21:38
  • 1
    What exactly are you looking to achieve? If you want to print all the values of the tuple, just unpacking with *a and passing the values as individual arguments works just fine, as you demonstrated. You can unpack the tuple and use it any way you like, including passing it to some function that performs additional formatting - but what formatting were you looking to apply? (note that (<value>, ...) is the norrmal representation of a tuple, so it makes a lot of sense for an f-string to use that) Commented Nov 6, 2024 at 21:38
  • 4
    Note that print has nothing to do with the unpacking. You can always unpack a tuple with * and one of its uses is to unpack it into the parameters of a function like print() which is what you're doing here. Commented Nov 6, 2024 at 21:43
  • 2
    NB: print doesn't remove commas and brackets. Those are just characters that would be generated when you would convert a tuple to string. But when you pass it *a, print doesn't see the tuple, but gets its members as separate arguments. There are no commas or brackets involved. Commented Nov 6, 2024 at 21:45
  • 1
    See also: stackoverflow.com/q/41896356/967621 Commented Nov 6, 2024 at 22:25

3 Answers 3

3

No, f-strings are of no use here.

Sign up to request clarification or add additional context in comments.

Comments

2

The most straightforward way I could come up with is the join method for strings.

>>> a = ('x','y','z') >>> print(f'{" ".join(a)}') x y z 

If your tuple elements aren't strings, they have to be converted (if possible).

>>> a = (0,-1,1) >>> print(f'{" ".join(map(str, a))}') 0 -1 1 

4 Comments

@Paul - It strikes me that putting this into an f-string is achieving nothing. At least in this example. print(" ".join(map(str, a)) is more concise, and print(*a) even more. F-strings are cool, but you don't need to use them for everything.
If this is something that someone wanted to use repeatedly within a program and needed to be able to do it in an f-string to combine with other f-string formatted text, it would probably be better to make a function which just returns " ".join(map(str, a)), and then you would call print(f"{unpack(a)}")
@StephenC I actually care about getting a string, which subsequently will be used in a file name. Printing just simplifies my post.
Well ... my_string = " ".join(map(str, a)) is simpler than my_string = f'{" ".join(map(str, a))}'. The f-string syntax is adding no value here. (Now if you were trying to construct the filename or pathname in one go, there would be some value, but that's not what the use-case you have described is doing.)
1

The problem is, that the asterisk (*) is no way to remove colons and braces in print function.

The asterisk is a way to treat a list or tuple not as a list or tuple. But each element of that as a single positiond paramter for the function you call.

This means that print(*a) is the same as print(a[0], a[1], a[2]). And print has the feature that it seperates each given argument with a single whitespace.

So no, there is no parameter unpacking functionality for f-strings. So you have to go with the normal ways of concatenate iterables:

from functools import reduce reduce(lambda left, right: f"{left} {right}", a) 

reduce will iterate over the elements and give in the first iteration element 0 and 1 to the lambda, in the second iteration result of first iteration and element 2 to the lambda and so on. This way many strings are created, but you can use f-strings. If you want to use them.

or a shortcut for that is (stolen from trincot's comment) " ".join(map(str, a)) here we use the builtin join-method of string. This concatenates an iterable of strings, so we have first to convert a into an iterable of strings, this is what map does, without changing the values of a.

A third way would be using print with a io.StringIo as destination, but this will not use f-strings, but here is a link to an example: python3 print to string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.