Problem No 15
Implement argument unpacking with *args and **kwargs
Medium≈ 16 minute session
Lesson guide
What this Python exercise practices
Implement argument unpacking with *args and **kwargs is a intermediate practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 16 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Intermediate
- Estimated time
- 16 minutes
Practice path
Summary
Practice using *args and **kwargs to accept variable positional and keyword arguments, then unpack and use them.
Problem statement
Write a function join_with_options(separator, *items, **options). - separator: a string used to join the items. - items: any number of positional values to be joined (convert to strings if needed). - options: may contain optional keys: - prefix: a string to prepend (default: empty string) - suffix: a string to append (default: empty string) - uppercase: boolean; if True, the joined core string should be converted to uppercase (default: False) Return the final formatted string. This exercise practices receiving arbitrary args/kwargs and unpacking/using them safely.
Task
Implement join_with_options(separator, *items, **options) that joins arbitrary items into a string using a separator and optional keyword options like prefix, suffix, and uppercase.
Examples
Join with prefix and suffix
Input
join_with_options('-', 'a', 'b', prefix='X', suffix='Z')
Output
Xa-bZ
Explanation
Items 'a' and 'b' are joined with '-', then prefixed with 'X' and suffixed with 'Z'.
Uppercasing
Input
join_with_options(' ', 'hello', 'world', uppercase=True)
Output
HELLO WORLD
Explanation
The joined core string 'hello world' is converted to uppercase because uppercase=True.
Input format
Call join_with_options with a separator, positional items, and optional keyword arguments like prefix, suffix, and uppercase.
Output format
Return a single formatted string produced by joining items and applying options.
Constraints
- Convert all items to strings before joining. - options may or may not include prefix, suffix, uppercase. - Do not assume the types of items (use str()).
Samples
Sample input 0
join_with_options(',', prefix='start', suffix='end')
Sample output 0
startend
Explanation 0
No items provided, so join yields an empty string and prefix/suffix are concatenated.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.