114

I have a sub-string:

substring = "please help me out"

I have another string:

string = "please help me out so that I could solve this"

How do I find if substring is a subset of string using Python?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
sunny
  • 1,143
  • 2
  • 7
  • 4

10 Answers10

171

with in: substring in string:

>>> substring = "please help me out"
>>> string = "please help me out so that I could solve this"
>>> substring in string
True
MarcoS
  • 13,386
  • 7
  • 42
  • 63
  • 11
    gosh, python is too strong, I was think it needs a function to do it, but it is a build-in one O-o – windsound Oct 13 '12 at 18:05
  • I just actually was learning JS after learning Python, for this you would need to add loads of if else statements and other things. So, I just wanted to remind myself of how its done in Python, this answer made me say to myself 'Of course!', I mean things like this in Python are just so trivial you never give them much thought! :P – Games Brainiac Jun 29 '13 at 13:58
  • @GamesBrainiac Actually, to do the same in JS it's just `string.indexOf(substring) != -1`, more [here](http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring-in-javascript) – LasagnaAndroid Feb 20 '14 at 20:42
  • Once you find, yes substring exists in string, how do you find at what position is the string, and what happens if the substring is in the string more than once? – yoshiserry May 27 '14 at 06:24
  • @yoshiserry if you want the position of `substring` in `string`, then you want yo use [string.index](http://stackoverflow.com/a/674775/162684) – MarcoS May 27 '14 at 09:31
21
foo = "blahblahblah"
bar = "somethingblahblahblahmeep"
if foo in bar:
    # do something

(By the way - try to not name a variable string, since there's a Python standard library with the same name. You might confuse people if you do that in a large project, so avoiding collisions like that is a good habit to get into.)

Amber
  • 507,862
  • 82
  • 626
  • 550
13

If you're looking for more than a True/False, you'd be best suited to use the re module, like:

import re
search="please help me out"
fullstring="please help me out so that I could solve this"
s = re.search(search,fullstring)
print(s.group())

s.group() will return the string "please help me out".

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ewegesin
  • 229
  • 3
  • 3
9

People mentioned string.find(), string.index(), and string.indexOf() in the comments, and I summarize them here (according to the Python Documentation):

First of all there is not a string.indexOf() method. The link posted by Deviljho shows this is a JavaScript function.

Second the string.find() and string.index() actually return the index of a substring. The only difference is how they handle the substring not found situation: string.find() returns -1 while string.index() raises an ValueError.

Samuel Li
  • 337
  • 5
  • 6
9

Thought I would add this in case you are looking at how to do this for a technical interview where they don't want you to use Python's built-in function in or find, which is horrible, but does happen:

string = "Samantha"
word = "man"

def find_sub_string(word, string):
  len_word = len(word)  #returns 3

  for i in range(len(string)-1):
    if string[i: i + len_word] == word:
  return True

  else:
    return False
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
QueenJolene
  • 114
  • 1
  • 3
  • 2
    Nice example, but your identation is incorrect. And to speed things up I'd check `if len(substring) > len(string) return False` also the loop range should better be `range(len(string)-len(substring))` because you won't find a three letter word in the last two letters of the string. (Saves a few iterations). – AnyOneElse Oct 22 '15 at 09:14
5

You can also try find() method. It determines if string str occurs in string, or in a substring of string.

str1 = "please help me out so that I could solve this"
str2 = "please help me out"
        
if (str1.find(str2)>=0):
  print("True")
else:
  print ("False")
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Abhishek Gupta
  • 4,066
  • 24
  • 27
1
def find_substring():
    s = 'bobobnnnnbobmmmbosssbob'
    cnt = 0
    for i in range(len(s)):
        if s[i:i+3] == 'bob':
            cnt += 1
    print 'bob found: ' + str(cnt)
    return cnt

def main():
    print(find_substring())

main()
avina k
  • 19
  • 1
1
In [7]: substring = "please help me out"

In [8]: string = "please help me out so that I could solve this"

In [9]: substring in string
Out[9]: True
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
0

Can also use this method

if substring in string:
    print(string + '\n Yes located at:'.format(string.find(substring)))
jackotonye
  • 3,537
  • 23
  • 31
0

enter image description here

Instead Of using find(), One of the easy way is the Use of 'in' as above.

if 'substring' is present in 'str' then if part will execute otherwise else part will execute.

Dcoder14
  • 1,831
  • 3
  • 12
  • 22