How to use the find function to help replace a word in a sentence?

Question:

I’m working on my Python homework of: Write a program that requests a sentence, a word in the sentence, and another word and then displays the sentence with the first word replaced by the second.

The hint says to use the method "find" to help solve it but i can’t think of a way to do so.

sentence = input("Enter a sentence.")
word = input("Enter word to replace. ")
replacement = input("Enter replacement word. ")

A = sentence.find(word)
print(sentencereplacement)

I’m not sure how to use find and what to print in the end.

Asked By: drc91

||

Answers:

It is literally called .replace()

result = sentence.replace(word, replacement)
Answered By: JohnyCapo

normally you can use replace method but if your teacher says so 🙂
We will just find where the word starts at the sentence and we will cut this word and place our new one.

sentence = input("Enter a sentence.")
word = input("Enter word to replace. ")
replacement = input("Enter replacement word. ")
start = sentence.find(word)
end = start + len(word)
print(sentence[0:start] + replacement + sentence[end : len(sentence)])

Best Regards,
Devrim

Answered By: Devrim Mert Yöyen