In this article we will discuss different ways to check if a sub-string exists in another string and if yes then find it’s index. Will also explore ways to check the existence of sub-string in case in-sensitive manner by ignoring case.


Check if a String contains another string or character using “in” and “not in” operator

in Operator

Python provides an in operator that can be used to check if a given character or string exists in another string. For example,

Output:

As our main string contains the sub-string ‘sample’ therefore in operator returned True.

not in Operator

Similarly we can also use “not in” operator to check the opposite scenario i.e. check if a string or character doesn’t exists in an another string. For example,

Output:

As our main string doesn’t contains the sub-string ‘Hello’ therefore ‘not in’ operator returned True.

Ignore case : Check if a String contains another string in case-insensitive manner

To check if a given string or a character exists in an another string or not in case insensitive manner i.e. by ignoring case, we need to first convert both the strings to lower case then use “ïn” or “not ïn” operator to check the membership of sub string.For example,

output:

Check if a String contains any string from a List

Suppose we have a list of strings and a big main string i.e.

Now we need to check if any of the string from the list exists in the main string or not. For this we have two techniques,
Technique 1 : Using for loop

Let’s create a function that will iterate through all the lists in given list and check for their membership in the main list i.e.

Now let’s use this function to check if any string from the list exists in the main string or not,

Output:

Well it worked, but creating a separate function with so many lines is not pythonic. What if we could do all this in a single line ?
Let’s see how to do that

Technique 1 : Using any() and List comprehension

In Python any(iterable) function accepts an iterable as an argument and returns if any element in the iterable is True. Let’s use any() to check if a given String contains any string from a List i.e.

Output:

We iterated over the list of strings using list of comprehension and created a new bool list and then passed it to any().

Check if a String contains all string from a List

In Python all(iterable) function accepts an iterable as an argument and returns if all element in the iterable are True. We can use List comprehension and all() to check if all elements from a list exists in the given string i.e.

Output:

Python Regex : Check if a String contains another string using regex

We can also use python’s regex module to check if a given string exists in another string in both case sensitive and insensitive manner i.e.

Case sensitive Approach:

Create a pattern to match string i.e.

Now search for the pattern inside the string for match using pattern.search(). If match is found then it will return a Match Object else None i.e.

Output:

Ignore case : check if a string exists in another string in case insensitive approach

Use re.search() to find the existence of a sub-string in the main string by ignoring case i.e.

Output:

Complete example is as follows,

Output:

 

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.