-1

For example if a string contains:

odfsdlkfn dskfThe Moonaosjfsl dflkfn

How can I check to see if it contains "The Moon"?

What I have currently been doing is (but does not work):

if string.find("The Moon")!=-1:
    doSomething

Is there anyway to do this?

Thanks!

DevinGP
  • 199
  • 2
  • 2
  • 10

2 Answers2

1

Simple:

string = 'odfsdlkfn dskfThe Moonaosjfsl dflkfn'
if 'The Moon' in string:
    dosomething
Alireza
  • 168
  • 1
  • 7
0

you could use regular expressions:

import re
text = "odfsdlkfn dskfThe Moonaosjfsl dflkfn"
if re.find("The Moon", text):
    ...

and in this case, you could ingore casing with re(pattern, text, re.IGNORECASE) if needed.

adrpino
  • 960
  • 8
  • 33
  • 1
    You might also want to explain when regex would be preferred over a simple `in`/`not in` check... – cs95 Dec 23 '17 at 17:22