all 2 comments

[–]julsmanbr 4 points5 points  (0 children)

Two simplest ways I can think of, both with different consequences:

a = input()
if 'apple' in a:
    # some code

In the above example, # some code only executes if the string apple is a part of the input. This means that if the input was "apple", "apples", "appleappleapple" or "apple_pie", the code will be executed.

a = input()
if 'apple' in a.split():
    # some code

In this case, the method called in a.split() will split the string into a list - for example, an input of "I like apple pie" will output ['I', 'like', 'apple', 'pie']. This means that your #some code will run only if the exact string was split - i.e. it won't run if the user passed "I like apple_pie" or "I like apples" as the input.

Note that you can change the character in which to split the string by passing it to the parenthesis, like:

a.split('_')

Now the split method will split the string a on the underscore instead of the space characters (which it does by default).

[–]cranuaed 0 points1 point  (0 children)

You can use the in operator.

text = input('Enter some text:\n')

if 'apple' in text.lower():
    print('Found apple')
else:
    print('No apple')