Your browser does not support JavaScript!
Upvotes Anonymous 2 User 0

What do a single star (*args) and double star (**kwargs) mean in Python?

In Python, you can create functions that accept a wide variety of parameters.
Single Star
The single-star (*) allows you to accept a varying number of positional arguments:
def get_name(*args):
    print('{0} {1} {2}'.format(args[0], args[1], args[2]))

get_name('John', 'A', 'Doe')
The output of running the code above is:
John A Doe
In the get_name function, args will be a tuple containing the values you passed into the function.
Double Star
The double-star (**) allows you to accept a varying number of keyword arguments (with a name and value) into a dictionary:
def get_name(**kwargs):
    print(kwargs['first'], kwargs['middle'], kwargs['last'])
    
get_name(first='John', last='Doe', middle='A')
The output of running the code is:
John A Doe
Helpful Links
Was this helpful?

Comments

Leave a Comment

I agree to the Terms of Service
Design © 2015, Downranked, LLC.,
Original user code contributions under MIT License