Skip to content
Related Articles

Related Articles

Improve Article

Python | Check if substring present in string

  • Last Updated : 24 Nov, 2018
Geek Week

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.




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

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.




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

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.




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

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.




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

Output :

for is present in GeeksforGeeks

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :