Python | Check if a Substring is Present in a Given String

24 Jun, 2022
In this article, we will cover how to check if a Python string contains another string or a substring in Python. 
Given two strings, check if a substring is there in the given string or not. 
Example 1: Input : Substring = "geeks" 
           String="geeks for geeks"
Output : yes
Example 2: Input : Substring = "geek"
           String="geeks for geeks"
Output : yes
Yes, Checking a substring is one of the most used tasks in python. Python uses many methods to check a string containing a substring like, find(), index(), count(), etc. The most efficient and fast method is by using an “in” operator which is used as a comparison operator. Here we will cover different approaches like:
Using the if... in 
Checking using the split() method
Using find() method
Using “count()” method
Using the index() method
Using  __contains__” magic class.
Using regular expressions 
Python3
# Take input from usersMyString1 = "A geek in need is a geek indeed" if "need" in MyString1:    print("Yes! it is present in the string")else:    print("No! it is not present")
Output:
Yes! it is present in the string
Checking if a substring is present in the given string or not without using any inbuilt function. First split the given string into words and store them in a variable s then using the if condition, check if a substring is present in the given string or not.
Python3
# Python code# To check if a substring is present in a given string or not # input strings str1 and substrstring = "geeks for geeks"  # or string=input() -> taking input from the usersubstring = "geeks"  # or substring=input() # splitting words in a given strings = string.split() # checking condition# if substring is present in the given string then it gives output as yesif substring in s:    print("yes")else:    print("no")
Output:
yes
We can iteratively check for every word, but Python provides us an inbuilt function find() which checks if a substring is present in the string, which is done in one line. find() function returns -1 if it is not found, else it returns the first occurrence, so using this function this problem can be solved. 
Python3
# function to check if small string is# there in big string  def check(string, sub_str):    if (string.find(sub_str) == -1):        print("NO")    else:        print("YES")  # driver codestring = "geeks for geeks"sub_str = "geek"check(string, sub_str)
Output:
YES
You can also count the number of occurrences of a specific substring in a string, then you can use the Python count() method. If the substring is not found then “yes ” will print otherwise “no will be printed”.
Python3
def check(s2, s1):    if (s2.count(s1) > 0):        print("YES")    else:        print("NO")  s2 = "A geek in need is a geek indeed"s1 = "geeks"check(s2, s1)
Output:
Yes
The .index() method returns the starting index of the substring passed as a parameter. Here “substring” is present at index 16.
Python3
any_string = "Geeks for Geeks substring "start = 0end = 1000any_string.index('substring', start, end)
Output:
16
Python String __contains__(). This method is used to check if the string is present in the other string or not. 
Python3
a = ['Geeks-13', 'for-56', 'Geeks-78', 'xyz-46']for i in a:    if i.__contains__("Geeks"):        print(f"Yes! {i} is containing.")
Output:
Yes! Geeks-13 is containing.
Yes! Geeks-78 is containing.
RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions. 
Python3
# When you have imported the re module,# you can start using regular expressions.import re # Take input from usersMyString1 = "A geek in need is a geek indeed"MyString2 = "geeks" # re.search() returns a Match object# if there is a match anywhere in the stringif re.search(MyString2, MyString1):    print("YES,string '{0}' is present in string '{1}'" .format(        MyString2, MyString1))else:    print("NO,string '{0}' is not present in string '{1}' " .format(        MyString2, MyString1))
Output:
NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 
Divyu_Pandey
rachitggupta2
tusharvickey1999
laxmigangarajula03
riya55
surajkumarguptaintern
Python string-programs
Python
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
How to Install PIP on Windows ?
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Different Methods to Reverse a String in C++