यहां हम फ़ंक्शन में निर्मित पायथन का उपयोग करते हैं। सबसे पहले हम वाक्य को शब्द की सूची में विभाजित करते हैं। फिर प्रत्येक शब्द को उलट दें और एक नई सूची बनाएं, यहां हम पायथन लिस्ट कॉम्प्रिहेंशन तकनीक का उपयोग करते हैं और आखिरी बार शब्दों की नई सूची में शामिल होकर एक नया वाक्य बनाते हैं।
उदाहरण
Input :: PYTHON PROGRAM Output :: NOHTYP MARGORP
एल्गोरिदम
Step 1 : input a sentence. And store this in a variable s. Step 2 : Then splitting the sentence into a list of words. w=s.split(“”) Step 3 : Reversing each word and creating a new list of words nw. Step 4 : Joining the new list of words and make a new sentence ns.
उदाहरण कोड
# Reverse each word of a Sentence
# Function to Reverse words
def reverseword(s):
w = s.split(" ") # Splitting the Sentence into list of words.
# reversing each word and creating a new list of words
# apply List Comprehension Technique
nw = [i[::-1] for i in w]
# Join the new list of words to for a new Sentence
ns = " ".join(nw)
return ns
# Driver's Code
s = input("ENTER A SENTENCE PROPERLY ::")
print(reverseword(s))
आउटपुट
ENTER A SENTENCE PROPERLY :: PYTHON PROGRAM NOHTYP MARGORP