While finding solutions to all string related problems, checking for the presence of a string inside another string is necessarily required.
One of the most common use cases is checking for invalid or not required characters in user input.
This post will explain a whole set of 5 different ways in which you can test if a string exists in another string in python.
Place
in
operator between the substring and the main string to check if one string contains another string. in will return True
if the string to test is present in the main string else it will be False
. Example, str = 'www.codippa.com' str_to_check = 'codippa' # check if string exists if str_to_check in str: print(str_to_check,'is present in',str) else: print(str_to_check,'is NOT present in',str)
Output will be
codippa is present in www.codippa.com
To remember the order of substring and string around in operator, remember that you are checking substring in a string.
find
method is present in string class and should be invoked on a string object. It accepts a single string argument and checks if the string supplied as argument is present in the string object on which this method is called.find
returns the index of beginning of argument string if it is present otherwise it returns -1. Example, str = 'www.codippa.com' str_to_check = 'codippa' # check the index of substring in main string index = str.find(str_to_check) if index != -1: print(str_to_check,'is present in',str,'starting at position',index) else: print(str_to_check,'is not present in',str)
Output of above code is
codippa is present in www.codippa.com starting at position 4
Remember that the index is counted from 0.
contains
method is present in string class. It is also invoked on a string object and accepts a single string argument.It checks for the presence of string argument in the string object on which this method is invoked and returns
True
if the substring is present else returns False
. Example, str = 'www.codippa.com' str_to_check = 'codippa' if str.__contains__(str_to_check): print(str_to_check, 'is present in', str) else: print(str_to_check, 'is not present in', str)
Output for the above code will be
codippa is present in www.codippa.com
Method 4: Using index method
index
is again a method of string class which is invoked on a string object.
This method accepts a string argument and checks if the argument string is present inside the string on which it is invoked.
If the string is present, then it returns the index of substring(start index will be 0) else it throws a ValueError.
Thus, for using this method, you need to apply some python exception handling concepts. Example,
str = 'www.codippa.com' str_to_check = 'codippa' # handle valueerror try: index = str.index(str_to_check) print(str_to_check,'is present in',str,'starting at position',index) except ValueError: print(str_to_check, 'is not present in', str)
Method 5: Using operator module
operator
module has a contains
function which accepts two arguments. First is the string in which you want to find the substring and second is the substring itself.
contains
will return True
if the substring is present, otherwise it will return False
. For using this method, you need to import operator
module. Example,
import operator str = 'www.codippa.com' str_to_check = 'codippa' if operator.contains(str, str_to_check): print(str_to_check, 'is present in', str) else: print(str_to_check, 'is not present in', str)
contains
method from operator
module internally uses in
operator.Hope the methods explained in this post will help you in testing if a string contains another string in python. Do not forget to hit the clap.