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.
# 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
Recommended Posts:
- Python | Check if a Substring is Present in a Given String
- Python | Check if element is present in tuple of tuples
- Python | Remove the given substring from end of string
- Python | Frequency of substring in given string
- Python | All occurrences of substring in string
- Python | Get the string after occurrence of given substring
- Python | Check if substring is part of List of Strings
- Python | Get the substring from given string using list slicing
- Python | Count overlapping substring in a given string
- Python | Ways to count number of substring in string
- Python | Ways to find nth occurrence of substring in a string
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
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.



