Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Split a sentence into words and join them

Split a sentence on whitespace into words and rejoin them with a given delimiter.

Python practice8 minString PatternsBeginnerLast updated March 18, 2026

Problem statement

Implement split_and_join(sentence, delimiter=' ') which splits sentence into words using whitespace (any sequence of spaces, tabs, newlines, etc.) and then joins those words using the provided delimiter. Leading/trailing whitespace should be removed. If the sentence has no words, return an empty string.

Task

Practice splitting on arbitrary whitespace and joining tokens, producing predictable normalized joins.

Examples

Multiple spaces

Input

split_and_join(' Hello world ')

Output

Hello world

Splitting removes extra whitespace, joining with default single space gives 'Hello world'.

Custom delimiter

Input

split_and_join('one\ttwo\nthree', ',')

Output

one,two,three

Tabs/newlines are treated as separators; join with comma.

Input format

A call to split_and_join(sentence, delimiter) where sentence is a string and delimiter is an optional string.

Output format

A single string returned: words joined by the delimiter, or empty string if no words.

Constraints

Do not use external libraries. Use Python string methods. Preserve word contents (do not remove punctuation).

Samples

Sample 1

Input

split_and_join(' a b c ')

Output

a b c

Extra spaces collapsed and words joined with a single space.