Search code examples
python-3.xformattingargumentsparameter-passingkeyword-argument

Passing a collection to print, to print with a separator in Python 3?


In Python 3, you can print a bunch of objects* with a variable number of objects as the first bunch of args:

For example:

print(192,168,178,42,sep=".")

Or for example:

print("09","03","2018",sep="-")

But like let's say I have a collection of [192,168,178,42] and I want to pass that to printed with a separator...how do I "unbox it into formal arguments" (and I'm using the term loosely) into arguments?


Solution

  • Use the * unpacking operator:

    print(*[192,168,178,42],sep=".")
    

    or with a variable:

    mylist = [192,168,178,42]
    print(*mylist, sep=".")
    

    Output:

    192.168.178.42
    

    See here for more details on packing/unpacking in Python.