Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained. python

# Python code to Reverse each word
# of a Sentence individually
  
# Function to Reverse words
def reverseWordSentence(Sentence):
  
    # Splitting the Sentence into list of words.
    words = Sentence.split(" ")
      
    # Reversing each word and creating
    # a new list of words
    # List Comprehension Technique
    newWords = [word[::-1] for word in words]
      
    # Joining the new list of words
    # to for a new Sentence
    newSentence = " ".join(newWords)
  
    return newSentence
  
# Driver's Code 
Sentence = "GeeksforGeeks is good to learn"
# Calling the reverseWordSentence 
# Function to get the newSentence
print(reverseWordSentence(Sentence))
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #Complete #function #accepts #string #reverses #word #All #spaces #string #python
ADD COMMENT
Topic
Name
6+3 =