0

I am trying to learn about * in python and encountered this problem, when I do:

print(*["John", 20])

I get following output:

John 20

I want to use it here and I know that this works:

print("My name is {0}, I am {1} lat".format(*["John", 20]))

But I don't understand how this works, as an argument of format I need: format("John", 20), not format("John" 20). Why in print statement I do not see this comma?

quamrana
  • 37,849
  • 12
  • 53
  • 71
Pobudi
  • 11
  • 1
  • 1
    The keyword to google you are looking for is "unpack operator", https://towardsdatascience.com/unpacking-operators-in-python-306ae44cd480?gi=1e634c5f4fcd – Fra93 Aug 10 '22 at 14:43
  • Note that it's not really an operator in the sense that `*["John", 20]` is not an *expression* that evaluates to a single value. Rather, it's part of various syntactic constructers (here, function call syntax), and it's meaning is to simply provide one argument per list element. Simply put, `print(*["John", 20])` and `print("John", 20)` are equivalent. It's the call to `print`, not the syntax, that outputs `John` and `20` separated by a single space. – chepner Aug 10 '22 at 15:12

0 Answers0