Using the "split" string method from the preceding lesson, complete the get_word function to return the {n}th word from a passed sentence. For example, get_word("This is a lesson about lists", 4) should return "lesson", which is the 4th word in this sentence. Hint: remember that list indexes start at 0, not 1
Answers
Answered by
1
Answer:
Script in Python
a="This is a lesson about lists"
split(a," ") ## this splits the string after every " " blank space
def get_word():
print(a[n]) ## n is the index value of whatever word you want to print. Just remember that "This" has index value 0, "is" has index value 1 and so on
get_word(a)
Answered by
3
Answer:
def get_word(sentence, n):
words = sentence.split(' ')
if n > 0 and n < len(words):
return(words[n - 1])
return("")
Explanation:
Similar questions