Skip to content
Related Articles

Related Articles

Reverse words in a given String in Python

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 16 Jun, 2022

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

Examples:

Input : str =” geeks quiz practice code”

Output : str = code practice quiz geeks  

Input : str = “my name is laxmi”

output : str= laxmi is name my 

reverse the words in the given string program 

Python3




# Python code
# To reverse words in a given string
 
# input string
string = "geeks quiz practice code"
# reversing words in a given string
s = string.split()[::-1]
l = []
for i in s:
    # apending reversed words to l
    l.append(i)
# printing reverse words
print(" ".join(l))


Output

code practice quiz geeks

Time Complexity: O(n), where n is the length of the string

Auxiliary Space: O(n), where n is the length of the string

 

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

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

This problem has an 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 a given string using split() method of string data type in python.
  • Reverse the word separated list.
  • Print words of the list, in string form after joining each word with space using ” “.join() method in python.

Python3




# 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))


Output

code practice quiz geeks

Time Complexity: O(n), where n is the length of the string

Auxiliary Space: O(n), where n is the length of the string

This solution use the same procedure but have different methods to reverse the words in string with the help backward iteration and regular expression module. Following are the steps of our approach:

  • Find all the words in string with the help of re.findall() function. 
  • Iterate over the list in backward manner. 
  • Join the words in a string with the help of join() function.

Python3




# Function to reverse words of string
import re
def rev_sentence(sentence):
 
    # find all the words in sentence
    words = re.findall('\w+', sentence)
 
    # Backward iterate over list of words and join using space
    reverse_sentence = ' '.join(words[i] for i in range(len(words)-1, -1, -1))
 
    # finally return the joined string
    return reverse_sentence
 
if __name__ == "__main__":
    input = 'geeks quiz practice code'
    print (rev_sentence(input))


Output

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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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!