Is there a way to extract text after a word is stated?

60 views Asked by At

So I am trying to make a virtual assistant and am working on a spelling feature like the one on a google home.

An example of what I am trying to achieve is when I say "hey google spell cat" it will say C A T How would I get cat into a variable?

I know how to split it

2

There are 2 answers

3
Cameron Chandler On BEST ANSWER

If I understand you correctly, you're saying that you have a string and wish to store the last word in it. This can be achieved by split as you said and then assignment:

text = 'hey google spell cat'
last_word = text.split()[-1]

If you instead want the word after spell you can just index spell and add one:

text = 'hi google spell cat for me'
split = text.split()
split[split.index('spell')+1]
0
IoaTzimas On

Try this:

given_text="hey google spell cat"
last_word=given_text.split()[-1]
reply=' '.join(list(last_word)).upper()

print(reply)

'C A T'