python program to reverse the words of sentence

In this example, you will learn to reverse string using split, reverse and join function.

In order to reverse words of string, we will take string as user input, first of all spilt string with whitespace and that will return list of words. then simply apply reverse function on list and at last join reverse list with white space.

Example :

# Program to reverse a words of string
userInput = input("Enter string to reverse :: ")

words = userInput.split()


print(' '.join(words))

Output :

Enter string to reverse :: I am Groot
Groot am I

Share your thoughts

Ask anything about this examples