Reputation: 65
I am trying to make a program that can check if two or more sub strings are in 1 string using python, can you tell me how can i do this
str = "I like apple do you like apples?"
if 'apples' or 'like' in str:
print('yes, I like apples')
Upvotes: 1
Views: 95
Reputation: 2780
The way to do this is:
if 'apples' in str or 'like' in str:
print('yes, I like apples')
The reason for this is that the or
operator will divide an if
statement into sections so with your original version:
if 'apples' or 'like' in str
you are essentially asking:
if 'apples'
or
if 'like' in str
and because if you have any if
statement with simply a string that string will equate to true
and so you are asking if true
or
if 'like' in str
and because true
is always true
it will be true even if neither substring is in the string you are searching.
Upvotes: 2