alittleboy
alittleboy

Reputation: 10956

find if string1 is contained in string2 in python

I have two strings: string1 = ABCDE and string2 = BC.

I hope to write a program returning a T/F value for whether string2 is contained in string1. For the example above, the function should return True. I know it's something like the %in% function in R, but since I am a newbie in Python, please share your thoughts on this simple question.

Thanks!

Upvotes: 0

Views: 3186

Answers (3)

bzdjamboo
bzdjamboo

Reputation: 565

string2.find(string1) != -1

would also work.

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113950

if string1 in string2: print "YES!"

this is really easy in python ;)

Upvotes: 1

RocketDonkey
RocketDonkey

Reputation: 37249

You could try this:

def StringTest(string1, string2):
  return string2 in string1

Or outside of a function:

result = string2 in string1

Either of these will return a boolean result.

Upvotes: 3

Related Questions