0
sentence = "There is a ship which is beautiful"
word = "hip"
if word in sentence:
       print("True")
else:
     print("False")

My output should be false there is no exact word hip.. Its matching based on ship

Levin Jose
  • 23
  • 2
  • You need to use `regex` [python-regex-to-match-a-specific-word](https://stackoverflow.com/questions/17090144/python-regex-to-match-a-specific-word) – Mohamad Ghaith Alzin Aug 17 '22 at 05:21

2 Answers2

2

Look into regex for more precise pattern matching, but I assume you are new so it might be a bit complicated. For now, this should work.

sentence = "There is a ship which is beautiful"
word = "hip"
if word in sentence.split(" "):
       print("True")
else:
     print("False")

Basically this code splits your sentence into an array of each word separated by a space, ["There","is",...,"beautiful"] then checks if word is an element of that array.

Joshua Lewis
  • 166
  • 4
0

You can make array first from a string. After that check value is in the array. So use split and after check words in array. https://www.w3schools.com/python/ref_string_split.asp

turtelian
  • 31
  • 4