How to determine whether a substring is in a different string

Question:

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?

Asked By: sunny

||

Answers:

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
Answered By: MarcoS
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
Answered By: Fredrik Pihl
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.)

Answered By: Amber

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

Answered By: ewegesin

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")
Answered By: Abhishek Gupta

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.

Answered By: Samuel Li

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
Answered By: QueenJolene
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()
Answered By: avina k

Can also use this method

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

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.

Answered By: Dcoder14
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.