Skip to content

Commit 2cff0b4

Browse files
Create reverse_words_in_sentence.py
1 parent e2a78d4 commit 2cff0b4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
reverse_words_in_sentence.py
3+
----------------------------
4+
Reverses the order of words in a given sentence.
5+
6+
Example:
7+
>>> reverse_words_in_sentence("hello world")
8+
'world hello'
9+
10+
>>> reverse_words_in_sentence("Python is fun")
11+
'fun is Python'
12+
"""
13+
14+
def reverse_words_in_sentence(sentence: str) -> str:
15+
"""
16+
Reverse the order of words in a sentence.
17+
Words are separated by spaces.
18+
"""
19+
words = sentence.strip().split()
20+
return " ".join(reversed(words))
21+
22+
23+
if __name__ == "__main__":
24+
sentence = input("Enter a sentence: ")
25+
print(reverse_words_in_sentence(sentence))

0 commit comments

Comments
 (0)