339

I have two strings and I would like to check whether the first is a substring of the other. Does Python have such a built-in functionality?

1

3 Answers 3

516

Try using in like this:

>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
Sign up to request clarification or add additional context in comments.

1 Comment

What about complexity as compare to using regex?
56

Try

isSubstring = first in theOther

Comments

43

string.find("substring") will help you. This function returns -1 when there is no substring.

2 Comments

What if I was looking for the position of the third or fourth occurrence of 'e', or the likes thereof?
python supports regulary expressions (regex). What ever you are looking for make a regex-group auto of it, afterwards you can select the 3rd group.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.