+ 1

Why doesn't this work?

tuple = ("Luis", "Heather", "Pedro", "Max") print("My friends are %s, %s and %s." % (tuple[0:2], tuple[3])) It sais there are not enough arguments for format string

16th Jan 2019, 9:36 PM
Lorenzo
Lorenzo - avatar
3 Answers
+ 6
Because the slice you did: tuple[0:1] results in a tuple (which has two string elements). And your formatted print statement expects 3 string parameters but you give only two (one tuple and one string). Try unpacking the slice and it will work. Just an extra asterisk like this: print("My friends are %s, %s and %s." % (*tuple[0:2], tuple[3]))
16th Jan 2019, 9:47 PM
Tibor Santa
Tibor Santa - avatar
+ 5
(tuple[0:2], tuple[3]) will result in (('Luis', 'Heather'), 'Max') (two values), but you need ('Luis', 'Heather', 'Max') (three values). 'tuple' shouldn't be used as a variable name, by the way
16th Jan 2019, 9:46 PM
Anna
Anna - avatar
- 1
In each assignment to the %s, you must send only one str, so the correct code is as follows: tuple = ["Luis", "Heather", "Pedro", "Max"] print('My friends are %s, %s, %s and %s.' % (tuple[0], tuple[1], tuple[2],tuple[3],))
18th Jan 2019, 8:47 PM
Ahmad Chitsazzade
Ahmad Chitsazzade - avatar