Variable Argument Lengths in Python Functions Using *args

By Hemanta Sundaray on 2022-01-06

In Python, there is an operator called the unpacking operator (*). The unpacking operator allows us to give our functions a variable number of arguments by performing what’s known as positional argument unpacking.

Below, we have a function that utilizes the unpacking operator:

def order(*args):
    print(args)

Notice that in our print() call, we simply use the name of args with the unpacking operator omitted. And the name of args is completely arbitrary.

If we called this function with random arguments:

print(order('Tomato Mozarella Sandwich', 'Cold Coffee'))

Our output would show us what is inside of *args.

('Tomato Mozzarella Sandwich', 'Cold Coffee')

Whatever name follows the unpacking operator will store the arguments passed into the function in the form of a tuple. This allows our functions to accept any number of arguments.

Join the Newsletter