Python | Reverse each word in a sentence
Last Updated :
15 Apr, 2023
Given a long sentence, reverse each word of the sentence individually in the sentence itself. Examples:
Input : Geeks For Geeks is good to learn
Output : skeeG roF skeeG si doog ot nrael
Input : Split Reverse Join
Output : tilpS esreveR nioJ
We shall use Python’s built in library function to reverse each word individually in the string itself. Prerequisites : 1. split() 2. Reversing Techniques in Python 3. List Comprehension Method in python 4. join()
- First split the sentence into list of words.
- Reverse each word of the string in the list individually.
- Join the words in the list to form a new sentence.
Below is the implementation of above idea.
Python3
def reverseWordSentence(Sentence):
words = Sentence.split(" ")
newWords = [word[:: - 1 ] for word in words]
newSentence = " ".join(newWords)
return newSentence
Sentence = "GeeksforGeeks is good to learn"
print (reverseWordSentence(Sentence))
|
Output:
skeeGrofskeeG si doog ot nrael
Python is well known for its short codes. We shall do the same task but with lesser line of codes.
Python3
def reverseWordSentence(Sentence):
return ' ' .join(word[:: - 1 ] for word in Sentence.split(" "))
Sentence = "Geeks for Geeks"
print (reverseWordSentence(Sentence))
|
Output:
skeeG rof skeeG
Approach#3: Using re.sub() function We can use re.sub() function to replace the words in sentence with reversed words in sentence. We capture the word in string with the help of group method and replace it with reversed word.
Python3
import re
def reverseWordSentence(Sentence):
newSentence = re.sub( '(\w+)' , lambda x : x.group()[:: - 1 ], Sentence)
return newSentence
Sentence = "GeeksforGeeks is good to learn"
print (reverseWordSentence(Sentence))
|
Output:
skeeGrofskeeG si doog ot nrael
Approach#4: Using map
We can split the sentence into individual words using the split() method, and then use the map() function to apply the [::-1] slice operator to each word.
Algorithm
1. Start by defining the function “reverse_words” that takes a single parameter “sentence”.
2. Split the “sentence” into individual words using the split function and store them in the variable “words”.
3. Define a lambda function that takes a string and returns its reverse. In this case, the lambda function is defined as “lambda x: x[::-1]”.
4. Apply the lambda function to each element in “words” using the map function and store the result in the variable “reversed_words”.
5. Join the elements in “reversed_words” into a single string with a space separating each element using the join function.
6. Return the resulting string
Python3
def reverse_words(sentence):
words = sentence.split()
reversed_words = list ( map ( lambda x: x[:: - 1 ], words))
return ' ' .join(reversed_words)
sentence = "GeeksforGeeks is good to learn"
print (reverse_words(sentence))
|
Output
skeeGrofskeeG si doog ot nrael
Time complexity: O(nm), where n is the number of words in the sentence and m is the maximum length of a word in the sentence.
Auxiliary Space: O(nm), as we are storing the reversed words in a list.
Please Login to comment...