Home »
Python
Python Multiple Function Arguments (*args and **kwargs)
Last Updated : April 25, 2025
Python allows you to pass multiple function arguments using *args
and **kwargs
, which provides flexibility in handling variable numbers of positional and keyword arguments.
Multiple Function Arguments
The *args and **kwargs is an approach to pass multiple arguments to a Python function. They allow to pass a variable number of arguments to a function. However, please note it is not necessary to name the variables as *args or **kwargs only. Only the * is important. We could also name the variables as *var or **named_vars.
Python *args (Variable-length arguments)
The *args is used to send a non-keyworded variable length argument list to a function.
Example
In this example, the function test_args
demonstrates how to handle multiple arguments using *args
, printing both the first argument and the additional ones passed as a list:
# Function to extract and print
# the multiple arguments
def test_args(arg1, *argv):
print("first argument :{}".format(arg1))
for arg in argv:
print("argument received :{}".format(arg))
# Function call
test_args("test1", "test2", "test3")
The output of the above code will be:
first argument :test1
argument received :test2
argument received :test3
Python **kwargs (Keyworded variable length arguments)
The **kwargs
allows to pass keyworded variable length arguments to a method. The **kwargs
is used in order to handle named arguments in a function.
Example
In this example, the function welcome_names
demonstrates how to use **kwargs
to handle an arbitrary number of keyword arguments and print them as key-value pairs:
# Function for demonstrating the
# use of **kwargs
def welcome_names(**kwargs):
if kwargs:
for key, value in kwargs.items():
print("{} = {}".format(key, value))
# Function call
welcome_names(name="include_help")
The output of the above code will be:
name = include_help
Another Example
In this example, we will print multiple arguments using the *args and **kwargs. Consider the below steps:
Define a function
Let's first define a function and write the print statements to print the arguments.
def test_multiple_args(arg1, arg2, arg3):
print(arg1)
print(arg2)
print(arg3)
Using *args
Now, let's use *args for the above function,
args = ['test1', 1,2]
test_multiple_args(*args)
It will print -
test1
1
2
Using **kwargs
Now, use **kwargs for the above function,
kwargs = {"arg3": 3, "arg2": "two", "arg1": 5}
test_multiple_args(**kwargs)
It will print -
5
two
3
Python Multiple Function Arguments Exercise
Select the correct option to complete each statement about handling multiple arguments in Python functions.
- In Python, you can define a function that accepts an arbitrary number of positional arguments using the ___ syntax.
- To pass a dictionary as keyword arguments to a function, you would use the ___ syntax.
- Which of the following is a correct way to call a function that accepts multiple arguments?
Advertisement
Advertisement