Editor

Reverse the Word Order

#33 · Strings · Beginner · 30 XP · about 5 min

Write reverse_words(sentence) that returns the words of sentence in reverse order, joined by single spaces. The letters inside each word stay in their original order.

A word is any run of characters without whitespace, so punctuation stays attached to its word. Extra spaces at the start, at the end or between words do not survive: the result never starts or ends with a space and never has two spaces in a row.

Examples

reverse_words("the sky is blue")    # returns 'blue is sky the'
reverse_words("  hello   world  ")  # returns 'world hello'
reverse_words("Wait, what?")        # returns 'what? Wait,'

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.