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()
words.reverse()
print(' '.join(words))
Output :
Enter string to reverse :: I am Groot
Groot am I
Ask anything about this examples