Assume that sentence is a variable that has been associated with a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Write the statements needed so that the variable secondWord is associated with the second word of the value of sentence . So, if the value of sentence were "Broccoli is delicious." your code would associate secondWord with the value "is" . (python)
my answer was
secondWord = sentence.substr(sentence.find(" ") + 1)
secondWord = secondWord.substr(0, secondWord.find(" "))
but i am keep on getting error message saying
⇒ You almost certainly should be using: [ ]
⇒ We think you might want to consider using: :
⇒ Solutions with your approach don't usually use: , (comma)
please help i am so stuck

Respuesta :

Answer:

The question asks for a program in Python. So i am providing Python code.

sentence = "Brocolli is delicious"

substring1 = sentence.find(' ')

substring2 = sentence.find(' ', substring1+1)

secondWord = sentence[substring1+1:substring2]

print(secondWord)

Explanation:

I will explain the code line by line.

First the sentence variable stores the string Brocolli is delicious.

find() function here is used to find the occurrence of the first space in the given sentence and this is stored in substring1. The first space occurs after the first word Brocolli of the given sentence.

substring2 = sentence.find(' ', substring1+1) In this statement find() function finds the occurrence of next space " " in the sentence after the first space which is stores in substring1. Now this indicates towards the end of second word is in the sentence because the second empty space comes after this word.

Lastly secondWord variable stores the second word of the line stored in sentence.

: operator shows the index of the substring1 + 1 which means that it starts from the substring1+1 of the sentence and ends at substring2 of the sentence. As we know substring1 has stored the position of the first space in the sentence so substring1+1 means the second word in the sentence i.e is. This will end at substring2 and we know that substring2 has stored the position of second empty space in the sentence. This is how the second word "is" is found and is stored in secondWord

At the end the value stored in secondWord i.e. is prints on the output screen.

The screen shot code along with output is attached.

Ver imagen mahamnasir
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE