Skip to content
Related Articles

Related Articles

Improve Article

Functions that accept variable length key value pair as arguments

  • Last Updated : 13 Aug, 2021

To pass a variable-length key-value pair as an argument to a function, Python provides a feature called **kwargs.
kwarg stands for Keyword arguments. It proves to be an efficient solution when one wants to deal with named arguments in their function.

Syntax:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

def functionName(**anything):
    statement(s)

Note: adding ‘**‘ to any term makes it a kwarg parameter. It accepts keywords as arguments. 



Example #1: 

Python3




# using kwargs
# in functions
 
 
def printKwargs(**kwargs):
    print(kwargs)
 
 
# driver code
if __name__ == "__main__":
    printKwargs(Argument_1='gfg', Argument_2='GFG')

Output:

{'Argument_1': 'gfg', 'Argument_2': 'GFG'}

Example #2:

Python3




# using kwargs
# in functions
 
 
def printValues(**kwargs):
    for key, value in kwargs.items():
        print("The value of {} is {}".format(key, value))
 
 
# driver code
if __name__ == '__main__':
    printValues(abbreviation="GFG", full_name="geeksforgeeks")

Output:

The value of abbreviation is GFG
The value of full_name is geeksforgeeks

Example #3:

Python3




# using kwargs
# in functions
# to concatenate
 
 
def concatenate(**arguments):
    # initialising empty string
    final_str = ""
     
    # Iterating over the Python kwargs
    # dictionary
    for elements in arguments.values():
        final_str += elements
    return final_str
 
 
# driver code
if __name__ == '__main__':
    print(concatenate(a="g", b="F", c="g"))

Output:

gFg

Example #4:

Python3




# using kwargs
# to multiply
 
 
def multiply(**kwargs):
   
    # initialising answer
    answer = 1
     
    # Iterating over the Python kwargs
    # dictionary
    for elements in kwargs.values():
        answer *= elements
    return answer
 
 
# driver code
if __name__ == '__main__':
    print(multiply(a=1, b=2, c=3, d=4, e=5))

Output:

120



My Personal Notes arrow_drop_up
Recommended Articles
Page :