328

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
  • 50,186
  • 60
  • 164
  • 235

3 Answers3

507

Try using in like this:

>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
Andrew Hare
  • 333,516
  • 69
  • 632
  • 626
54

Try

isSubstring = first in theOther
Martin Stone
  • 12,247
  • 1
  • 38
  • 52
41

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

cs95
  • 330,695
  • 80
  • 606
  • 657
Daniel Wehner
  • 2,029
  • 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