Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find whether the word is exact match in sentence

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading