336

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?

snakile
  • 52,936
  • 62
  • 169
  • 241

3 Answers3

513

Try using in like this:

>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
55

Try

isSubstring = first in theOther
Martin Stone
  • 12,682
  • 2
  • 39
  • 53
43

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

cs95
  • 379,657
  • 97
  • 704
  • 746
Daniel Wehner
  • 2,159
  • 1
  • 18
  • 22
  • What if I was looking for the position of the third or fourth occurrence of 'e', or the likes thereof? – Musixauce3000 Apr 14 '16 at 22:37
  • 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. – Cutton Eye Feb 08 '18 at 15:35