Jump To Right Section
Show
Suppose, You want to reverse a sentence. On this tutorial, we need to focus on learn how to create a python program to reverse a sentence with solely 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 6 7 |
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 2 3 |
arrives nonetheless future it from run it dread |
This program could be additional be compressed.
1 2 3 4 |
sentence = "dread it run from it future nonetheless arrives" print(" ".be part of(sentence.break up()[::-1])) |
Output
1 2 3 |
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 6 7 |
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 3 4 |
sentence = enter("Enter a sentence :") print(" ".be part of(reversed(sentence.break up()))) |
Output
1 2 3 4 |
Enter a sentence :That is an enter enter an is This |
Leave a Comment