Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 4

Create a function to join words with a separator

Easy

6 minute session

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.

Code editor
Loading editor…

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.

07:41 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.