0

What is this star mark in this print function call?

for i in range(int(input())):   
    s=input()  
    print(*["".join(s[::2]),"".join(s[1::2])])  
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
NPE
  • 432
  • 5
  • 13

1 Answers1

0

It is called argument unpacking. If you were to omit it, then it would only give the list created by the list comprehension to the print function as one argument. With the asterisk it passes every item in that list as separate argument. Consider this example:

def my_func(arg1, arg2, arg3):
    print('yay it worked')

and then call it with:

my_func(*[1, 2, 3])

that way arg1 will be 1, arg2 will be 2 and arg3 will be 3. If you change the call to:

my_func([1, 2, 3])

then you pass the list to arg1 and it will raise a TypeError because it's missing two positional arguments.

Matthias Schreiber
  • 2,347
  • 1
  • 13
  • 20