How to Check if a String contains a Substring in Python

In this Python tutorial, we will discuss several methods on How to Check if a String contains a Substring in Python. Moreover, we’ll look at a variety of examples Check if a String contains a Substring in Python.

Recently, I am working on a machine learning project and I have found that it requires some sub-string elements and checking whether it is available in string or not. So, I have done some research and found that we have to Check if a String contains a Substring in Python.

Here we will learn:

  • How to Check if a String contains a Substring in Python using in operator.
  • Check if a String contains a Substring in Python using find()
  • How to Check if a String contains a Substring in Python using count()
  • Check if a String contains a Substring in Python using index method
  • How to Check if a String contains a Substring in Python using regular expression
  • Check if a string contains a substring in python using operator.contains()
  • How to check if a string contains a substring in python using list comprehension
  • How to check if a string contains a substring in python using split()

Python checks if a string contains sub-string

Within a String, a substring is a group of characters. In Python, there are primarily eight methods that are commonly used and important to understand while checking if a string contains a sub-string in Python.

How to Check if a String contains a Substring in Python using IN operator

  • In this section, we will discuss how to check if a String contains a Substring in Python using in operator.
  • Python supports in operators for strings. Thus, we may use it to determine whether or not a string is a part of another string.
  • This method will return true if the substring contains in string otherwise it will return false.

Syntax:

Let’s have a look at the syntax and understand the working of in operator in Python.

sub in str

Example:

Here we will take an example and how to check if a String contains a Substring in Python using in operator.

Source Code:

Country_name=" 'U.S.A', 'Germany' ,'Australia' "
sub_string="U.S.A"
# Using in operator
result= sub_string in Country_name
# Display the Content
print("Check substring is availabe or not :", result)

In the following given code first, we created a string named Country_name. Next, we defined a variable ‘sub_string’ and then used the ‘in’ operator to check whether the substring exists in the given dictionary or not.

Here is the implementation of the following given code.

How to Check if a String contains a Substring in Python using in operator
How to Check if a String contains a Substring in Python using in operator

This is how to Check if a String contains a Substring in Python using IN operator.

Read: Python Naming Conventions

How to Check if a String contains a Substring in Python using find()

  • Now let us see How to Check if a String contains a Substring in Python using find().
  • To determine whether a string contains a substring, you can alternatively use other methods, such as string.find(). The index of the substring in the string is returned by string.find(substring).
  • The string.find() function returns a non-negative number if the substring is present in the original string. This can be used as a condition to check whether the string contains the substring.

Syntax:

Here is the Syntax of string.find() function in Python

string.find(substring)

Note: This function takes only one parameter which is a substring.

Example:

Here we will take an example and check whether the String contains a Substring in Python using find().

Source Code:

new_str = "U.S.A, Australia, Germany"
new_sub_str = 'Australia'
if new_str.find(new_sub_str) > -1:
    print('It contains substring.')
else :
    print('sub-String does not contain.')

In the above code first, we defined the string and used the str.find() function, and set the condition if the given substring is less than -1 then it will display sub-string does not contain in string.

Here is the implementation of the following given code

How to Check if a string contains a Substring in Python using find
How to Check if a string contains a Substring in Python using find

As you can see in the Screenshot we have discussed how to Check if a string contains a Substring in Python using find().

Read: Ask for user input in Python

How to Check if a String contains a Substring in Python using count()

  • In this section, we will discuss How to Check if a String contains a Substring in Python using count().
  • The count() method searches a string for instances of a substring. It gives 0 if the substring is not present in the string.
  • The substring’s length will be indicated by the method’s return value. This implies that the procedure will return 0 if the substring isn’t present.

Syntax:

Let’s have a look at the syntax and understand the working of count() function in Python

string.count(substring)

Note: This function takes only one parameter which is a substring.

Example:

Let’s take an example and check if a String contains a Substring in Python using count().

Source Code:

cars_name = 'BMW, Volkswagen, Ford-150'

if cars_name.count('Volkswagen'):
    print('String contains the substring')
else:
    print("String doesn't contain substring")

In the following given code, we used the count() function and within this function, we set the substring to it. Next, we used the if-else condition and set if the input substring contains the string or not.

You can refer to the below Screenshot

How to Check if a String contains a Substring in Python using count
How to Check if a String contains a Substring in Python using count

In this example, we have understood how to Check if a String contains a Substring in Python using count.

Read: Check if a number is a prime Python

How to Check if a String contains a Substring in Python using index method

  • In this section, we will discuss How to Check if a String contains a Substring in Python using an index.
  • A substring in a string is recognized using the procedure. If the substring is missing from the string, the function generates a ValueError rather than returning anything.

Syntax:

Here is the Syntax of the index method in Python

string.index(substring)

Note: This function takes only one parameter which is a substring.

Example:

Let’s take an example and check if a String contains a Substring in Python using count().

Source Code:

cities_in_USA = "NewYork,  Los Angeles, Chicago"
try :  
    result = cities_in_USA.index("Los Angeles") 
    print ("Los Angeles contains in the string.") 
except : 
    print ("Los Angeles does not contain in the string.") 

Here is the Screenshot of the following given code

How to Check if  a String contains a Substring in Python using index method
How to Check if a String contains a Substring in Python using index method

As you can see in the Screenshot we have discussed how to Check if a String contains a Substring in Python using index method.

Read: How to add two variables in Python

How to Check if a String contains a Substring in Python using regular expression

  • Now let us discuss How to Check if a String contains a Substring in Python using regular expression.
  • Regular expressions offer a more versatile method of pattern matching on strings.
  • The first instance of a regular expression sequence will be found using the re.search() function, which will then return it. All rows of the supplied string will be checked.
  • The re.search() method will easily match a substring pattern.

Example:

Let’s take an example and check if a String contains a Substring in Python using regular expression.

Source Code:

from re import search

Country_name = "U.S.A , Germany, Australia"
substring = "Australia"

if search(substring, Country_name):
    print (" Substring exist in string!")
else:
    print (" Substring does not exist in string!")

In the above code, we have used the ‘re’ module for applying the re.search() function and set the condition if the input substring is available in the string or not.

You can refer to the below Screenshot

How to Check if a String contains a Substring in Python using regular expression
How to Check if a String contains a Substring in Python using regular expression

This is how to Check if a String contains a Substring in Python using regular expression.

Read: How to make a matrix in Python

How to check if a string contains a substring in python using operator.contains()

  • In this section, we will discuss How to check if a string contains a substring in python using the operator.contains().
  • Depending on whether the string object contains the specific string object or not, the contains() method in the Python String class returns a Boolean value as True or False.

Syntax:

Here is the Syntax of an operator.contains() in Python.

operator.contains(string,substring)

Note: This function takes two parameters string and substring.

Example:

Here we will take an example and check if a string contains a substring in python using operator.contains().

Source Code:

import operator 
   
new_str = "Python is a programming language"
   
if operator.contains(new_str, "programming"): 
    print ("programming exists in the string.") 
else : 
    print ("programming does not exist in the string.") 

In the above code, we used the operator.contains() function and assigns the string as well as substring as an argument.

Here is the execution of the following given code.

How to check if a string contains a substring in python using operator.contains
How to check if a string contains a substring in python using operator.contains()

This is how to check if a string contains a substring in python using operator.contains().

Read: Python program to find the sum of n numbers

How to check if a string contains a substring in python using list comprehension

  • Here we will discuss how to check if a string contains a substring in python using list comprehension.
  • To determine whether a string contains a substring from a list or not, list comprehension is frequently used as a solution. To determine if we can discover a match in this instance, we search for both list and string items, and if we do, the result is true.

Example:

Let’s take an example and check if a string contains a substring in python using list comprehension.

Source Code:

state_name_in_USA = 'Alaska, Arizona, Alabama'

search_substring = ['Arizona']

print("Input string: ", state_name_in_USA)

new_output = [i for i in search_substring if(i in state_name_in_USA)]

print("Substring contain in string?: " + str(bool(new_output))) 

How to use list comprehension to determine whether a text contains a list element is shown in the code above. First, the string is initialized under the name state name in USA.

Following that, the test list (called search substring) was initialized as well. Before running the function, we printed the initial string and list. Then, we checked the string to see if it contained the list element using list comprehension, and we reported the outcome.

Here is the Screenshot of the following given code

How to check if a string contains a substring in python using list comprehension
How to check if a string contains a substring in python using list comprehension.

Read: Python Program for even or odd

How to check if a string contains a substring in python using split()

  • Now let us understand How to check if a string contains a substring in python using split().
  • This function is used to split the string into a substring after splitting the given string by the specified separator.
  • To determine whether a substring is included in the provided string or not, first, divide the given string into words and store them in a variable called result.

Example:

Here we will take an example and check if a string contains a substring in python using split().

Source Code:

Bikes_in_USA = 'Aprilia Kawasaki Ducati'
substring = "Kawasaki" 

result = Bikes_in_USA.split()
if substring in result:
	print("substring contain in string")
else:
	print("It does not contain in string")

In the following given code first, we used the split() function and it will break into the substring. Next, we set the condition if the substring contains the string or not.

Here is the implementation of the following given code

How to check if a string contains a substring in python using split
How to check if a string contains a substring in python using split

Also, take a look at some more Python tutorials.

In this article, we have discussed several methods on How to Check if a String contains a substring in Python. Also, we have covered the following given topics

  • How to Check if a String contains a Substring in Python using in operator.
  • How to Check if a String contains a Substring in Python using find()
  • How to Check if a String contains a Substring in Python using count()
  • How to Check if a String contains a Substring in Python using the index method
  • How to Check if a String contains a Substring in Python using regular expression
  • How to check if a string contains a substring in python using the operator.contains()
  • How to check if a string contains a substring in python using list comprehension
  • How to check if a string contains a substring in python using split()