In this article, we'll take a look at
Hide
Suppose You want to Python Program To Reverse a Sentence. In this tutorial, we need to focus on learning how to create a Python program to reverse a sentence in three easy steps.
Algorithm
- Take a string as enter.
- Convert the sentence into a list of phrases.
- Be part of the record within the reverse order, which finally is the reversed sentence.
Program
1 2 3 4 5 |
sentence = "dread it run from it future nonetheless arrives" word_list = sentence.break up() reversed_list = word_list[:: -1] reversed_sentence = " ".be part of(reversed_list) print(reversed_sentence) |
Output
1 |
arrives nonetheless future it from run it dread |
This program could be compressed.
1 2 |
sentence = "dread it run from it future nonetheless arrives" print(" ".be part of(sentence.break up()[::-1])) |
Output
1 |
arrives nonetheless future it from run it dread |
Python lists could be reversed utilizing the reversed()
technique, which can be utilized rather than record[::-1]
in this system as follows.
1 2 3 4 5 |
sentence = "dread it run from it future nonetheless arrives" word_list = sentence.break up() reversed_list = reversed(word_list) reversed_sentence = " ".be part of(reversed_list) print(reversed_sentence) |
Program for user-provided enter
1 2 |
sentence = enter("Enter a sentence :") print(" ".be part of(reversed(sentence.break up()))) |
Output
1 2 |
Enter a sentence :That is an enter enter an is This |
Leave a Comment