Kwargs Function in Python

Level 3108 keystrokes

This defines a function with **kwargs to accept any number of keyword arguments as a dictionary.

Python code

# **kwargs collects keyword arguments into a dictionary
1def greet(**kwargs):
# items() yields each (name, value) pair in the order they were passed
2 for key, value in kwargs.items():
# Prints one line per argument, e.g. "name: Alice"
3 print(f"{key}: {value}")
4
# kwargs becomes {"name": "Alice", "age": 30}
5greet(name="Alice", age=30)

Output

name: Alice
age: 30
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 4
  • (× 4
  • )× 4
  • :× 3
  • ,× 2
  • {× 2
  • }× 2
  • *× 2
  • =× 2
  • .× 1

More Python examples at level 3

← Python typing practice