Created
May 30, 2020 15:10
-
-
Save pratik7368patil/69d95fc6cc80ad1ee3f307b989b6c045 to your computer and use it in GitHub Desktop.
Reverse words in sentence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This problem can be solved using split() and join() | |
| def reverse_words(sen): | |
| words = sen.split(' ') # split sentence into words | |
| # then reverse a list of words and join them as a sentence | |
| string = ' '.join(reversed(words)) | |
| print(string) | |
| sen = "python is love" | |
| reverse_words(sen) | |
| # the output will be "love is python" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment