Reverse the sentence in Python

Problem Statement:

The problem is that we need to reverse the sentence in python. We cannot really reverse it using the conventional method of reversing a string because the problem includes not just reversing the string but keeping in mind the spaces between two words and just reversing the order of the words in the given sentence. For example, if the given sentence is “Copyassignment is a good platform to learn Python” then the output should be “Python learn to platform good a is Copyassignment”.

Code to reverse the sentence in python:

sentence = str(input("Enter your sentence: "))
list_of_words = sentence.split(" ")
new_sentence = ""

new_reversed_list = list_of_words[::-1]

for i in new_reversed_list:
    new_sentence = new_sentence + i + " "

print(new_sentence)

Output:

Code:

code to reverse the sentence in python

Output:

output

Also Read:

Share:

Author: Ayush Purawr