Search code examples
pythonpython-3.xstringsubstring

Check if word is contained exactly in a Python string


I have this case:

a = "I run away without you"
if "with" in a:
    print("s")
else:
    print("n")

This expression is True. But, I want to print "n", because a has to have exactly "with" and not a substring. How can I do this?


Solution

  • Maybe you mean this:

    "with" - True
    "without" - False
    "with xxx" - True
    

    you can try: if "with" in a.split(" "):