The syntax is the * and **. The names *args and **kwargs are only by convention but there's no hard requirement to use them.

You would use *args when you're not sure how many arguments might be passed to your function, i.e. it allows you pass an arbitrary number of arguments to your function. For example:

>>> def print_everything(*args):
        for count, thing in enumerate(args):
...         print( '{0}. {1}'.format(count, thing))
...
>>> print_everything('apple', 'banana', 'cabbage')
0. apple
1. banana
2. cabbage

Similarly, **kwargs allows you to handle named arguments that you have not defined in advance:

>>> def table_things(**kwargs):
...     for name, value in kwargs.items():
...         print( '{0} = {1}'.format(name, value))
...
>>> table_things(apple = 'fruit', cabbage = 'vegetable')
cabbage = vegetable
apple = fruit

You can use these along with named arguments too. The explicit arguments get values first and then everything else is passed to *args and **kwargs. The named arguments come first in the list. For example:

def table_things(titlestring, **kwargs)

You can also use both in the same function definition but *args must occur before **kwargs.

You can also use the * and ** syntax when calling a function. For example:

>>> def print_three_things(a, b, c):
...     print( 'a = {0}, b = {1}, c = {2}'.format(a,b,c))
...
>>> mylist = ['aardvark', 'baboon', 'cat']
>>> print_three_things(*mylist)
a = aardvark, b = baboon, c = cat

As you can see in this case it takes the list (or tuple) of items and unpacks it. By this it matches them to the arguments in the function. Of course, you could have a * both in the function definition and in the function call.

Answer from David Webb on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_args_kwargs.asp
Python *args and **kwargs
Arbitrary Arguments are often shortened to *args in Python documentation.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ args-kwargs-python
*args and **kwargs in Python - GeeksforGeeks
Below code shows how *args collects multiple positional arguments into a tuple and how **kwargs collects keyword arguments into a dictionary. ... # *args example def fun(*args): return sum(args) print(fun(5, 10, 15)) # **kwargs example def ...
Published ย  September 20, 2025
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ args-and-kwargs
Python *args and **kwargs (With Examples)
Inside the function, we have a loop which adds the passed argument and prints the result. We passed 3 different tuples with variable length as an argument to the function. Python passes variable length non keyword argument to function using *args but we cannot use this to pass keyword argument.
๐ŸŒ
Real Python
realpython.com โ€บ python-kwargs-and-args
Python args and kwargs: Demystified โ€“ Real Python
November 7, 2023 - This is where *args can be really ... the following example: ... def my_sum(*args): result = 0 # Iterating over the Python args tuple for x in args: result += x return result print(my_sum(1, 2, 3))...
๐ŸŒ
Python Tips
book.pythontips.com โ€บ en โ€บ latest โ€บ args_and_kwargs.html
1. *args and **kwargs โ€” Python Tips 0.1 documentation
*args is used to send a non-keyworded ... to help you get a clear idea: def test_var_args(f_arg, *argv): print("first normal arg:", f_arg) for arg in argv: print("another arg through *argv:", arg) test_var_args('yasoob', 'python', 'eggs', 'test')...
Top answer
1 of 11
1829

The syntax is the * and **. The names *args and **kwargs are only by convention but there's no hard requirement to use them.

You would use *args when you're not sure how many arguments might be passed to your function, i.e. it allows you pass an arbitrary number of arguments to your function. For example:

>>> def print_everything(*args):
        for count, thing in enumerate(args):
...         print( '{0}. {1}'.format(count, thing))
...
>>> print_everything('apple', 'banana', 'cabbage')
0. apple
1. banana
2. cabbage

Similarly, **kwargs allows you to handle named arguments that you have not defined in advance:

>>> def table_things(**kwargs):
...     for name, value in kwargs.items():
...         print( '{0} = {1}'.format(name, value))
...
>>> table_things(apple = 'fruit', cabbage = 'vegetable')
cabbage = vegetable
apple = fruit

You can use these along with named arguments too. The explicit arguments get values first and then everything else is passed to *args and **kwargs. The named arguments come first in the list. For example:

def table_things(titlestring, **kwargs)

You can also use both in the same function definition but *args must occur before **kwargs.

You can also use the * and ** syntax when calling a function. For example:

>>> def print_three_things(a, b, c):
...     print( 'a = {0}, b = {1}, c = {2}'.format(a,b,c))
...
>>> mylist = ['aardvark', 'baboon', 'cat']
>>> print_three_things(*mylist)
a = aardvark, b = baboon, c = cat

As you can see in this case it takes the list (or tuple) of items and unpacks it. By this it matches them to the arguments in the function. Of course, you could have a * both in the function definition and in the function call.

2 of 11
523

One place where the use of *args and **kwargs is quite useful is for subclassing.

class Foo(object):
    def __init__(self, value1, value2):
        # do something with the values
        print value1, value2

class MyFoo(Foo):
    def __init__(self, *args, **kwargs):
        # do something else, don't care about the args
        print 'myfoo'
        super(MyFoo, self).__init__(*args, **kwargs)

This way you can extend the behaviour of the Foo class, without having to know too much about Foo. This can be quite convenient if you are programming to an API which might change. MyFoo just passes all arguments to the Foo class.

๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ argparse.html
argparse โ€” Parser for command-line options, arguments and subcommands
Arguments read from a file must be one per line by default (but see also convert_arg_line_to_args()) and are treated as if they were in the same place as the original file referencing argument on the command line. So in the example above, the expression ['-f', 'foo', '@args.txt'] is considered equivalent to the expression ['-f', 'foo', '-f', 'bar'].
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what are args** and kwargs** and __somethinghere__ in python?
r/learnpython on Reddit: What are args** and kwargs** and __somethinghere__ in python?
October 15, 2024 -

Hello everyone, I hope you all are doing well. Iโ€™m confused about these keywords in Python and what they do and where I can use them, since am new to python.

Anyone?

Top answer
1 of 10
75
Some reading
2 of 10
43
Symmetrically double-underscored names ('dunders') like __this__ aren't anything special in and of themselves. This is just a convention used by Python devs to mark things that are 'magic' in some way (usually, other builtin Python things rely on them in some way, e.g. __init__() is called when constructing a class, or __repr__() is called when printing). args and kwargs aren't special either; it's the asterisks (*foo and **bar) that are the magic bits. They are related and are used to make working with call signatures nicer, for either positional or keyword arguments, respectively. Sometimes you're not sure how many parameters a function will need to receive - sometimes it's one, sometimes it's twenty. For example, a function like sum() - you could lock it to only ever adding two numbers, sum(a, b), but that's not super nice for the users - we'd likely prefer to add ALL the numbers someone puts in, right? In more oldschool languages, a canonical solution would be to take some kind of a list of numbers, e.g. sum(numbers), and that works - but now your users have to slap extra braces around things, e.g. sum([1, 2, 3, 4]), which is annoying. Instead, the single-star syntax before an argument in a function signature (in our case, changing sum(numbers) => sum(*numbers)) flags to Python to wrap any positional arguments (that don't look like they belong to an earlier, non-starry arg) into a tuple under the hood. When passing inputs into a function, this is reversed - if you have anything listey and the function expects a bunch of positional arguments (starry or not, doesn't matter), if you put in an asterisk before the list, Python will unpack that into individual positional arguments. Double-star for kwargs does the same, except for named arguments - it implicitly wraps/unwraps dictionaries-and-friends like one-star wraps/unwraps tuples-and-friends.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ argparse.html
Argparse Tutorial โ€” Python 3.14.3 documentation
$ python prog.py usage: prog.py [-h] [-v] x y prog.py: error: the following arguments are required: x, y $ python prog.py -h usage: prog.py [-h] [-v] x y positional arguments: x the base y the exponent options: -h, --help show this help message and exit -v, --verbosity $ python prog.py 4 2 -v 4^2 == 16 ยท Notice that so far weโ€™ve been using verbosity level to change the text that gets displayed. The following example instead uses verbosity level to display more text instead:
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-args-and-kwargs-in-python-3
How To Use *args and **kwargs in Python 3 | DigitalOcean
March 6, 2026 - Learn how to use *args and **kwargs in Python 3 to write flexible functions, covering variable arguments, unpacking operators, decorators, and inheritance.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_function_arbitrary_arguments.asp
Python *args
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python basics โ€บ python *args
Explain Python *args Clearly By Practical Examples
March 31, 2025 - def add(*args): total = 0 for arg in args: total += arg return total total = add(1, 2, 3) print(total)Code language: Python (python) ... If you use the *args argument, you cannot add more positional arguments. However, you can use keyword arguments. The following example results in an error ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_command_line_arguments.htm
Python - Command-Line Arguments
This script's help now shows one positional argument in the form of 'user'. The program checks if it's value is 'Admin' or not and prints corresponding message. C:\Python311>python parser2.py --help usage: parser2.py [-h] user sample argument parser positional arguments: user options: -h, --help show this help message and exit
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ 10 examples to master *args and **kwargs in python
10 Examples to Master *args and **kwargs in Python | Towards Data Science
January 20, 2025 - The following function prints the passed arguments accordingly. def arg_printer(a, b, *args): print(f'a is {a}') print(f'b is {b}') print(f'args are {args}') arg_printer(3, 4, 5, 8, 3) a is 3 b is 4 args are (5, 8, 3) The first two values are ...
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ python โ€บ args-and-kwargs
Python *args and **kwargs (With Example)
November 5, 2025 - Learn about Python *args and **kwargs with examples. Understand how to use these flexible arguments to write more dynamic and reusable functions.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_arguments.asp
Python Function Arguments
The following example has a function with one argument (fname).
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python โ€บ *args and **kwargs in python
*args and **kwargs in Python - Scaler Topics
November 16, 2023 - We can use *args to allow for flexibility in the number of arguments during object creation. We can use **kwargs to handle additional properties of the rectangle, such as colour or label. ... The single and double asterisks that we use are called unpacking operators. Unpacking operators are used to unpack the variables from iterable data types like lists, tuples, and dictionaries. A single asterisk(*) is used on any iterable given by Python.
๐ŸŒ
ListenData
listendata.com โ€บ home โ€บ python
What are *args and **kwargs and How to use them
In this example, we would show you multiple ways to include keyword arguments in dataframe. The most straightforward way to include arguments is to pass them in apply( ) function as named in user-defined function.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ args-and-kwargs-in-python
How to Use *args and **kwargs in Python
March 23, 2022 - Let's define a function to add two numbers in Python. We'll write it like this: ... What if you need to add three numbers? Simple, we can modify the function to accept three arguments and return their sum as:
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ function-argument
Python Function Arguments (With Examples)
In the above example, we have created the function find_sum() that accepts arbitrary arguments. Notice the lines, ... Here, we are able to call the same function with different arguments. Note: After getting multiple values, numbers behave as an array so we are able to use the for loop to access each value. ... Before we wrap up, letโ€™s put your knowledge of Python function arguments to the test!