In: Computer Science
In PYTHON:
Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence.
To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
Answer : please up vote or comment for any query i will update the same . Thanks
Note : check attached image for output and program .
Program Plan :
Program Python 3,7 compiler :
def getLastWord(sentence):
List= sentence.split() #split sentence by space
LastWord = str(List[len(List)-1]) #convert last word in
string
return LastWord
lastWord = getLastWord("hello tinku jangid how are you.") #store return word
print ( "Last Word is :",lastWord ) #print last word
Program python 2 compiler
def getLastWord(sentence):
List= sentence.split() #split sentence by space
LastWord = str(List[len(List)-1]) #convert last word in
string
return LastWord
lastWord = getLastWord("hello tinku jangid how are you.") #store return word
print "Last Word is :",lastWord #print last word
Output :
Python 2 compiler output & program