How do I check if a string contains another substring in Python

Sometimes we need to find out whether a particular string is present in another string or not. So to know that there are some already pre-defined methods available in Python programming. There are most common five methods which we can use are:
  1. in operator
  2. find method
  3. index method
  4. count method
  5. regular expression method

So, let’s start to discuss each method in detail.

Method 1: Using in operator

The first method which we are going to see is the ‘in’ operator method. This python operator will return True if the substring is present in the string, or else it will return False. This is the very easiest way to check whether any string has a substring or not. The below program will give a complete example to use this method.

string = "Python coding day"

substring = "Python"


if substring in string:

    print("Found the substring")

else:

    print("Not Found")

Output:

Found the substring

Line 1 and 2: We created a string and a substring.

Line 4 to 7: We are now checking conditions with the ‘in’ operator to see whether there is a substring in the string or not. If it returns True, it will print the statement, or it will jump to the other statement. Our output shows that it returns True, or we can also see that Python is present in the string.

Method 2: Using the find () method

The second method which we are going to discuss is the find () method. This method will return the first index of the substring if the substring is present in the string, or else it will return -1. This is also the very easiest way to check whether any string has a substring or not. The below program will give a complete example of how to use this method.

string = "Python coding day"

substring = "Python"

 
if string.find(substring) != -1:

    print("Found the substring")

else:

    print("Not Found")

Output:

Found the substring

Line 1 and 2: We created a string and a substring.

Line 4 to 7: We are now checking conditions with the find method to see whether there is a substring in the string or not. As we know, if the substring is present, then it will return the starting index of the substring, else it will return -1. So, we are checking the condition that the print will execute when the return value is not equal to -1, which directly means that a substring is present in the string. Our output shows that it returns a positive value, or we can also see that Python is present in the string.

Method 3: Using the index method

The next method which we are going to discuss is the index () method. This method is very similar to the find () method, but this method will return the first index of the occurrence substring if the substring is present in the string, or else it raises an error. So, to handle the value error exception, we have to use exception handling as shown in the below example program. This is also the very easiest way to check whether any string has a substring or not. The below program will give a complete example of how to use this method.

string = "Python coding day"

substring = "Python"


try:

    string.index(substring)

except ValueError:

    print("Not Found")

else:

    print("Found the substring")

Output:

Found the substring

Line 1 and 2: We created a string and a substring.

Line 4 to 7: We kept our string checking condition inside of the try and except block to handle the exception error; otherwise, the program will stop unconditionally. We are now checking with the string class index method to find out the first index value of the substring occurrence. As we know, if the substring is present, then it will return the starting index of the substring; else, it will raise an exception. If the substring is present in the string, it will directly jump to the else part; otherwise, it will raise the exception ValueError. Our output shows that it returns a positive value, or we can also see that Python is present in the string.

Method 4: Using count () method

The next method which we are going to discuss is the count () method. The count method has one more advantage: it will count all the substrings present in the string. It will return the number of substrings present in the string. If no substring is present in the string, then it will return 0.

string = "Python coding day"

substring = "Python"

 
if string.count(substring) >0:

    print("Found the substring")

else:

    print("Not Found")

Output:

Found the substring

Line 1 and 2: We created a string and a substring.

Line 4 to 7: We are now checking conditions with the string count class method to see whether there is a substring in the string or not. As we know, if the substring is present, it will return the total number of the substrings present in the string; otherwise, it will return 0. So, we check the condition that the print will execute when the return value is greater than the 0, which directly means that a substring is present in the string. Our output shows that it returns a value greater than 0, or we can also see that Python is present in the string.

The count method is useful to know the total number of substrings occurrences in the main string.

Method 5: Using regular expression method

The next method which we are going to discuss is the regular expression method. The regular expression method is very easy to use. The regular expression first defines the pattern which we want to search, and then we have to use the search method, which is re library class. We pass both the search pattern and the original string inside that, as shown in the example program below.

from re import search


string = "Pythoncodingday"

substring = "Python"


if search(substring, string):

    print("Found the substring")

else:

    print("Not found")

Output:

Found the substring

Line 1: We import the re library because we need the search module.

Line 3 and 4: We created a string and a substring.

Line 6 to 9: We are now checking conditions with the search module whether there is a substring in the string or not. If it returns True, it will print the statement or jump to the other statement. Our output shows that it returns True, or we can also see that Python is present in the string.

Conclusion: In this article, we have seen different major types of methods to search for a substring in the parent string. The last method of the regular expression, which we discussed, is very slow, and we have to use this in some critical cases only. The best method which is very easy to use is the ‘in’ operator method. Other methods are also easy to use that depend upon the requirements of the user. So you can see where you want to use these methods according to your requirements.

The code for this article is available at the Github link:
https://github.com/shekharpandey89/check-string-has-substring-or-not-python



from https://ift.tt/3AIuJXO

Post a Comment

0 Comments