Home > Software design >  How to find whether the word is exact match in sentence
How to find whether the word is exact match in sentence

Time:08-18

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related