Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Reverse words in a given String in Python

  • Difficulty Level : Easy
  • Last Updated : 23 Nov, 2020

We are given a string and we need to reverse words of a given string?


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input : str = geeks quiz practice code
Output : str = code practice quiz geeks

This problem has existing solution please refer Reverse words in a given String link. We will solve this problem in python. Given below are the steps to be followed to solve this problem.

  • Separate each word in given string using split() method of string data type in python.
  • Reverse the word separated list.
  • Print words of list, in string form after joining each word with space using ” “.join() method in python.

# Function to reverse words of string 
def rev_sentence(sentence): 
    # first split the string into words 
    words = sentence.split(' '
    # then reverse the split string list and join using space 
    reverse_sentence = ' '.join(reversed(words)) 
    # finally return the joined string 
    return reverse_sentence 
if __name__ == "__main__"
    input = 'geeks quiz practice code'
    print (rev_sentence(input))


code practice quiz geeks

This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!