*args and **kwargs in Python

*args – The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list.

**kwargs – The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).

Sample Program using both *args and **kwargs

def f(*args, **kwargs):
    if kwargs.get('reverse_str') == True:
        return [i[::-1].title() for i in args]
    else:
        return [i.title() for i in args]

a = ['sayan', 'dey']
print(f(*a, reverse_str = True))

Leave a comment