mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
18 lines
397 B
Python
18 lines
397 B
Python
def reverse_words(sentence: str) -> str:
|
|
"""Reverse the order of words in a given string.
|
|
|
|
Extra whitespace between words is ignored.
|
|
|
|
>>> reverse_words("I love Python")
|
|
'Python love I'
|
|
>>> reverse_words("I Love Python")
|
|
'Python Love I'
|
|
"""
|
|
return " ".join(sentence.split()[::-1])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|