-1

This if statement works...when I'm only looking for one string

    body = json.dumps(work['Body'])[2:-2]
    if len(something) >= 1 and 'SMILE' in body:
           print " i'm happy"

But this one does not when i'm looking for multiple strings....

    body = json.dumps(work['Body'])[2:-2]
    if len(something) >= 1 and 'SMILE' or 'LAUGH' or 'LOVE' or 'CHEER' in body:
           print " i'm still happy "

What gives? How can I have multiple string in the if condition to match against another string?

  • 2
    `or` isn't a conjunction in Python; it's a boolean operator. It doesn't do anywhere near the amount of stuff the English conjunction "or" does. – user2357112 May 19 '14 at 06:09

1 Answers1

2

Assuming body is some string, you can do as follows using any:

# for example
body='SMILE_LAUGH_SOMESTRING_WHTATEVER'
if len(something) >= 1 and any(s in body for s in ('SMILE', 'LAUGH', 'LOVE', 'CHEER')):
     print("i'm still happy")
Tim
  • 41,901
  • 18
  • 127
  • 145
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • what would be the opposite... so that i want to say "not in"... i want to have another if statement that says do xyz if those keywords are not in the string? – user3467359 May 19 '14 at 06:08
  • You could use [all](https://docs.python.org/2/library/functions.html#all) also. Or `not all(...)` or any combination depending what you want. – Marcin May 19 '14 at 06:12
  • @user3467359 `and not any(` would be the opposite – Tim May 19 '14 at 06:15