+ 2

Format tuples in python

If I have this: t = (1, 3, 2, 2, 3) f = “{0} {1} {2} {3} {4}”.format(t) it gives an error This is the only way I was able to do it: t = (1, 3, 2, 2, 3) f = “{0} {1} {2} {3} {4}”.format(t[0], t[1], t[2], t[3], t[4]) Any suggestions? If not it’s ok it is just a minor problem

6th Feb 2019, 6:13 PM
Ben Landon
Ben Landon - avatar
4 odpowiedzi
+ 4
Alternatively you can use format(*t) - the star 'unpacks' your tuple.
6th Feb 2019, 6:51 PM
HonFu
HonFu - avatar
+ 4
Another option for the same result, to separate the values by spaces: f = ' '.join(map(str,t))
6th Feb 2019, 8:48 PM
Tibor Santa
Tibor Santa - avatar
+ 3
HonFu that worked thank you!
6th Feb 2019, 6:58 PM
Ben Landon
Ben Landon - avatar
+ 2
Just write f = "{}".format(t)
6th Feb 2019, 6:37 PM
Théophile
Théophile - avatar