Problem No 4
Create a function to join words with a separator
Easy≈ 6 minute session
Lesson guide
What this Python exercise practices
Create a function to join words with a separator is a beginner practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Practice path
Summary
Write a function that joins a sequence of items into a single string using a provided separator.
Problem statement
Create a function join_with_separator(words, sep) that takes an iterable of items (strings, numbers, etc.) and a separator string sep. The function should convert each item to its string representation and join them with the separator. If the iterable is empty, return an empty string. The function must accept lists, tuples, and other iterables.
Task
Implement join_with_separator(words, sep) that converts items to strings and joins them with sep. Handle empty sequences and different iterable types.
Examples
Join words with a space
Input
join_with_separator(['hello', 'world'], ' ')
Output
hello world
Explanation
Each item is a string; they are joined with a single space.
Input format
An iterable (list/tuple/other) of items as the first argument and a string separator as the second argument.
Output format
A single string containing the joined items.
Constraints
Do not assume all items are strings. Convert items to strings before joining. The separator is always a string.
Samples
Sample input 0
join_with_separator(['a', 'b', 'c'], ',')
Sample output 0
a,b,c
Explanation 0
The list items are joined by a comma.
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.