Python: To Check If String Contains a Substring

Do you want to check if String contains a substring or string contains a word in Python? This article will show you how to check if the string contains a specific Word or Substring with examples.

Using In Operator With If Condition

You can check directly in Python using the if condition and in operator to check whether the substring is present in the given string.

The In operator in Python can be treated as similar to the contains method present in the other programming language for example JAVA. And In operator works similar to contains method.

Let us see the below example code the usage of if condition and in operator to check the substring is contained in a string or not.

#Initializing the String
givenString = "Hi You are Visiting Coduber.com Website."

#Using if and in condition to check susbtring
if "Coduber.com" in givenString:
      print("Substring is Present.")
else:
      print("Substring is not present.")

Output:

Substring is Present.

As you can see in the above code, the substring that I have chosen is present in the given string, and hence using the if condition and in operator, it successfully detects that string contains the substring.

Using Regex In Python

You can use the regex library of Python. Regex provides the search method which can be helpful in verifying if the substring is present in the string or not.

Let us see in the below example code how you can use the regex library.

#importing Regex
import re

#Initializing the String
givenString = "Hi You are Visiting Coduber.com Website."


#Creating a Substring to Find in given String
subString = "Visiting"


#Checking if the above Substrings are present in the 
#givenString or not
if re.search(subString, givenString):
      print("Substring Found in the Given String.")
else:
      print("Substrings not found in given String.")

Output:

Substring Found in the Given String.

As you can see in the above code, the regex search method takes two arguments one is the substring and the other is the original string. And it returns true if the substring is present in the given string else it will return false.

Python: To Check If String Contains a Substring

Using string.find Method

The String class of Python has a method named find that can be used on the given string to find if the given subString or Word is present or not.

Let us see in the below example code the usage of the find method of String Class in Python.

#Initializing the String
givenString = "Hi You are Visiting Coduber.com Website."


#Creating a Substring to Find in given String
subString = "What"


#Checking if the above Substrings are present in the 
#givenString or not
if givenString.find(subString)!=-1:
      print("Substring Found in the Given String.")
else:
      print("Substrings not found in given String.")

Output

Substrings not found in given String.

In the above example, we have taken the word that is not present in the original string and hence our code returns that this substring or word is not present in the string. Find method returns -1 when the substring is not present and it returns 1 when the substring is found in the string.

Does Python Have A String Contains Substring Method?

Yes, Python has the methods to verify or check if the substring is present in the string or not, and all the methods discussed above can be used that act similar to contains Method.

Wrap Up

I hope you were able to check if the given word is present in the string using any one of the methods above. Note that from the above method the first method is the most recommended and efficient method to use.

There are one or two more methods that are not listed here as those are not recommended and should not be used for good coding practices. Let me know if you have a better solution to the above problem in the comments and I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python: How To Check If a String Contains a List of Substring
  2. How To Return Null in Python Discussed
  3. How to Split a String into a List of Words or Letters in Python
  4. Reading A Binary File In Python With Examples

Leave a Reply

Your email address will not be published. Required fields are marked *