Get startedGet started for free

Arbitrary keyword arguments

Arbitrary positional arguments are one way to add flexibility when creating custom functions, but you can also use arbitrary keyword arguments.

Your goal is to take the concat function that you created in the last exercise and modify it to accept arbitrary keyword arguments. Good luck!

This exercise is part of the course

Intermediate Python for Developers

View Course

Exercise instructions

  • Define concat() as a function that accepts arbitrary keyword arguments called kwargs.
  • Inside the function, create an empty string.
  • Inside the function, loop over the keyword argument's values, using kwarg as the iterator.
  • Call concat() with keyword arguments of start equal to "Python", middle equal to "is", and end equal to "great!".

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define a function called concat
____
  
  # Create an empty string
  result = ____
  
  # Iterate over the Python kwargs
  ____:
    result += " " + kwarg
  return result

# Call the function
print(____(____="Python", ____="is", ____="great!"))
Edit and Run Code