Python | Check if substring present in string

Lets solve this general problem of finding if a particular piece of string is present in a larger string in different ways. This is a very common kind of problem every programmer comes across aleast once in his/her lifetime. This article gives various techniques to solve it.

Method 1 : Using in operator
The in operator is the most generic, fastest method to check for a substring, the power of in operator in python is very well known and is used in many operations across the entire language.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to demonstrate 
# checking substring in string
# using in operator
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using in to test
# for substring
print ("Does for exists in GeeksforGeeks ? : ")
if "for" in test_str :
    print ("Yes, String found")
else
    print ("No, String not found")

chevron_right


Output :



Does for exists in GeeksforGeeks ? : 
Yes, String found

Method 2 : Using str.find()
str.find() method is generally used to get the lowest index at which the string occurs, but also returns -1, if string is not present, hence if any value returns >= 0, string is present, else not present.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to demonstrate 
# checking substring in string
# using str.find()
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using str.find() to test
# for substring
res = test_str.find("for")
if res >= 0:
    print ("for is present in GeeksforGeeks")
else :
    print ("for is not present in GeeksforGeeks")

chevron_right


Output :

for is present in GeeksforGeeks

Method 3 : Using str.index()
This method can be used to performs the similar task, but like str.find(), it doesn’t return a value, but a ValueError if string is not present, hence catching the exception is the way to check for string in substring.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to demonstrate 
# checking substring in string
# using str.index()
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using str.index() to test
# for substring
try
    res = test_str.index("forg")
    print ("forg exists in GeeksforGeeks")
except :
    print ("forg does not exists in GeeksforGeeks")

chevron_right


Output :

forg does not exists in GeeksforGeeks

Method 4 : Using operator.contains()
This is lesser known method to check for substring in a string, this method is also effective in accomplishing this task of checking a string in a string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to demonstrate 
# checking substring in string
# using operator.contains()
import operator
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using operator.contains() to test
# for substring
if operator.contains(test_str, "for"):
    print ("for is present in GeeksforGeeks")
else :
    print ("for is not present in GeeksforGeeks")

chevron_right


Output :

for is present in GeeksforGeeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.