Program to reverse a sentence word by word in Python
In this python programming article, we are going to learn
-
program to reverse a sentence word by word in python
Program to reverse a sentence word by word in Python
The program is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
sentence = input("Enter sentence: ").strip()
sentence = sentence.lower()
word_list = sentence.split(" ")
reversed_word_list = word_list[::-1]
reversed_sentence = " ".join(reversed_word_list)
print("The reversed sentence is:",reversed_sentence)
The output of the program is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter sentence: I love python programming
The reversed sentence is: programming python love i
Few important tips about the program
1. We take the string input from the user and convert it to lowercase.
2. Then we split the sentence word by word and make a list out of it.
3. We have then reversed the list using negative indexing.
4. We have then joined the reversed list to a string which is actually our reversed sentence.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Oct 13,2022