Functions that accept variable length key value pair as arguments
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:
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 functionsdef printKwargs(**kwargs): print(kwargs)# driver codeif __name__ == "__main__": printKwargs(Argument_1='gfg', Argument_2='GFG') |
Output:
{'Argument_1': 'gfg', 'Argument_2': 'GFG'}Example #2:
Python3
# using kwargs# in functionsdef printValues(**kwargs): for key, value in kwargs.items(): print("The value of {} is {}".format(key, value))# driver codeif __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 concatenatedef 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 codeif __name__ == '__main__': print(concatenate(a="g", b="F", c="g")) |
Output:
gFg
Example #4:
Python3
# using kwargs# to multiplydef multiply(**kwargs): # initialising answer answer = 1 # Iterating over the Python kwargs # dictionary for elements in kwargs.values(): answer *= elements return answer# driver codeif __name__ == '__main__': print(multiply(a=1, b=2, c=3, d=4, e=5)) |
Output:
120
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


