Split and Rejoin a Sentence

Easy ⏱ 8 min 85% acceptance ★★★★★ 4.7
Write rejoin_with_dashes(s) that splits a sentence into words on whitespace and rejoins them using a single hyphen - as the separator, collapsing any run of extra spaces along the way.

Examples

Example 1
Input
s = 'Data Vix rocks'
Output
'Data-Vix-rocks'
Explanation

Words joined with hyphens instead of spaces.

Example 2
Input
s = 'too   many   spaces'
Output
'too-many-spaces'
Explanation

Calling split() with no arguments collapses repeated whitespace.

Constraints

  • 1 <= len(s) <= 1000

Topics

Stringssplit/join

Companies

AmazonAdobe

Hints

Hint 1

`s.split()` with no arguments splits on any whitespace run and drops empty tokens.

Hint 2

Then `"-".join(words)`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.