0

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!

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I don't know the in operator in Python...that's indeed very convenient.
@alittleboy No worries - it is definitely a good one to know :)
@Ant Haha, yeah - I misread it at first and through 'program' said 'function'.
1
if string1 in string2: print "YES!"

this is really easy in python ;)

1 Comment

technically, he wanted to know if string2 is in string1, so: Wrong! :-P
1
string2.find(string1) != -1

would also work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.