MCPcopy
hub / github.com/TheAlgorithms/Python / reverse_words

Function reverse_words

strings/reverse_words.py:1–11  ·  view source on GitHub ↗

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'

(sentence: str)

Source from the content-addressed store, hash-verified

1def reverse_words(sentence: str) -> str:
2 """Reverse the order of words in a given string.
3
4 Extra whitespace between words is ignored.
5
6 >>> reverse_words("I love Python")
7 'Python love I'
8 >>> reverse_words("I Love Python")
9 'Python Love I'
10 """
11 return " ".join(sentence.split()[::-1])
12
13
14if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected