'How to determine whether a substring is in a different string [duplicate]

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?



Solution 1:[1]

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

Solution 2:[2]

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.)

Solution 3:[3]

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".

Solution 4:[4]

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.

Solution 5:[5]

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

Solution 6:[6]

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")

Solution 7:[7]

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

Solution 8:[8]

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()

Solution 9:[9]

Can also use this method

if substring in string:
    print(string + '\n Yes located at:'.format(string.find(substring)))

Solution 10:[10]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MarcoS
Solution 2 Amber
Solution 3 the Tin Man
Solution 4 Samuel Li
Solution 5 the Tin Man
Solution 6 Guy Avraham
Solution 7 Fredrik Pihl
Solution 8 avina k
Solution 9 jackotonye
Solution 10 Dcoder14