Vinz1396
Vinz1396

Reputation: 35

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?

Upvotes: 1

Views: 976

Answers (2)

Zxd2009
Zxd2009

Reputation: 127

Maybe you mean this:

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

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

Upvotes: 2

Coko
Coko

Reputation: 1

I am not sure I understand the problem you are having but if what you are trying to do is check whether your variable only contains the string "with", then use == instead of in

Upvotes: -1

Related Questions