How to determine whether a substring is in a different string

PythonString

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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 - Python

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 - Python

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 - Python

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 - Python

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 - Python

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 - Python

Can also use this method

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

Solution 10 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsunnyView Question on Stackoverflow
Solution 1 - PythonMarcoSView Answer on Stackoverflow
Solution 2 - PythonAmberView Answer on Stackoverflow
Solution 3 - PythonewegesinView Answer on Stackoverflow
Solution 4 - PythonSamuel LiView Answer on Stackoverflow
Solution 5 - PythonQueenJoleneView Answer on Stackoverflow
Solution 6 - PythonAbhishek GuptaView Answer on Stackoverflow
Solution 7 - PythonFredrik PihlView Answer on Stackoverflow
Solution 8 - Pythonavina kView Answer on Stackoverflow
Solution 9 - PythonjackotonyeView Answer on Stackoverflow
Solution 10 - PythonDcoder14View Answer on Stackoverflow