Which 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 badges298 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.3k7 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

491k81 gold badges618 silver badges546 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 badges298 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 badges298 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")

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

Guy Avraham

3,2943 gold badges37 silver badges46 bronze badges

answered Sep 27, 2014 at 14:29

Which 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.6k7 gold badges82 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,39519 silver badges28 bronze badges

Which 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,7013 gold badges11 silver badges22 bronze badges

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

Use the in keyword to check if the substring is present in the string in the first place. The in keyword returns a Boolean value – a value that is either True or False . The in operator returns True when the substring is present in the string. Using the in keyword is a helpful first step before using the find() method.

Which method determines whether a certain substring is present anywhere in a string?

The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.

Which method could be used to strip specific characters from the end of a string?

TrimEnd. The String. TrimEnd method removes characters from the end of a string, creating a new string object. An array of characters is passed to this method to specify the characters to be removed.

What is the return value of the string method Lstrip ()?

Return Value from lstrip() lstrip() returns a copy of the string with leading characters stripped. All combinations of characters in the chars argument are removed from the left of the string until the first mismatch.