What method would you use to determine whether a certain substring is present in a string?

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

156k41 gold badges209 silver badges297 bronze badges

asked Sep 9, 2011 at 11:55

0

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 Sep 9, 2011 at 11:57

MarcoSMarcoS

13.2k6 gold badges40 silver badges62 bronze badges

5

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 Sep 9, 2011 at 11:57

AmberAmber

488k81 gold badges617 silver badges545 bronze badges

1

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

156k41 gold badges209 silver badges297 bronze badges

answered Nov 23, 2011 at 14:09

ewegesinewegesin

2293 silver badges3 bronze badges

2

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 Jan 19, 2015 at 20:40

Samuel LiSamuel Li

3275 silver badges6 bronze badges

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

156k41 gold badges209 silver badges297 bronze badges

answered Mar 27, 2015 at 19:08

1

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

What method would you use to determine whether a certain substring is present in a string?

Guy Avraham

3,2743 gold badges37 silver badges46 bronze badges

answered Sep 27, 2014 at 14:29

What method would you use to determine whether a certain substring is present in a string?

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 Sep 9, 2011 at 11:57

Fredrik PihlFredrik Pihl

43.5k7 gold badges81 silver badges130 bronze badges

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 Jun 23, 2015 at 3:30

1

Can also use this method

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

answered Nov 21, 2016 at 15:38

jackotonyejackotonye

3,37619 silver badges28 bronze badges

What method would you use to determine whether a certain substring is present in a string?

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 May 7, 2018 at 11:42

Dcoder14Dcoder14

1,6713 gold badges11 silver badges22 bronze badges

Which method would you use to determine whether a substring is present in a string?

Using index() The string. index() method returns the starting index of the substring passed as a parameter. However, a major con is that it returns a ValueError in case the substring does not exist. We can solve this by using a Try Except.

Which method would you use to determine whether a certain substring is present in a string quizlet?

Which method would you use to determine whether a certain substring is present in a string? The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters.

Which method would you use to determine whether a certain substring is a suffix?

Which method would you use to determine whether a certain substring is the suffix of a string? In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.

Which of the following string methods can be used to determine if a string contains only \n?

Which of the following string methods can be used to determine if a string contains only '\n' characters? The right side of the '*' must be an integer.