Skip to content
Related Articles

Related Articles

Python | Check if substring present in string

Improve Article
Save Article
  • Last Updated : 12 Aug, 2022
Improve Article
Save Article

Let us 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 atleast 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. 

Python3




# 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. 
 

Python3




# 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. 
 

Python3




# 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. 
 

Python3




# 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

Method#5: Using String.count() function: This function is used to count the presence of element is the string. We can use this function to check the existence of string in large string. If string exist in string then it return some number else it return 0. 

Python3




# Python 3 code to demonstrate
# checking substring in string
# using String.count()
 
# initializing string
test_str = "GeeksforGeeks"
 
# Substring
temp = "for"
# using String.count to test
# for substring
ans = test_str.count(temp)
# Printing our result
if ans:
    print ("for is present in GeeksforGeeks")
else :
    print ("for is not present in GeeksforGeeks")

Output:

for is present in GeeksforGeeks

Method#6: Using re.search() function: re.search() function is used to search for an pattern in string. We can use sub-string as a pattern to search in string. 

Python3




# Python 3 code to demonstrate
# checking substring in string
# using re.search()
import re
# initializing string
test_str = "GeeksforGeeks"
 
# Substring
temp = "forg"
# using re.search to test
# for substring
ans = re.search(temp, test_str)
# Printing our result
if ans:
    print ("forg is present in GeeksforGeeks")
else :
    print ("forg is not present in GeeksforGeeks")

Output:

forg is not present in GeeksforGeeks

Method: Using list comprehension 

Python3




s="geeks for geeks"
s2="geeks"
print(["yes" if s2 in s else "no"])

Output

['yes']

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!