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
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
arrives nonetheless future it from run it dread
This program could be compressed.
sentence = "dread it run from it future nonetheless arrives"
print(" ".be part of(sentence.break up()[::-1]))Output
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.
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
sentence = enter("Enter a sentence :")
print(" ".be part of(reversed(sentence.break up())))Output
Enter a sentence :That is an enter enter an is This
