Check Python string in string with contains, find and % functions

Wondering how to check for a substring in with Python string in, contains, and find functions? Here’s a guide that explains all functions with examples.

There are several methods in python that can resolve this problem. Let’s discuss each method more deeply with explanations, and programs.

We have written a few more articles if you are also wondering, how to read a file line by line, and how to write text to a file.

Starting with all the approaches and explanations.

How to text contains a string in string in Python

Using in Operator

in operator in python is used for checking whether a string contains any other substring or not.

Let’s try to understand it with an easy program.

Syntax : 

Substring in String

The in operator returns the value true or false if the string contains a substring then it will return true if not then returns false.

Example code :

string1 = ‘we all love python programming language’
string2 = ‘python’
string3 = ‘like’
string4 = ’ java ’

print(f’ ” {string1}” contains the substring  “{string2}” = {string2 in string1}’)
print(f’ ” {string1}” contains the substring  “{string3}” = {string2 in string1}’)
print(f’ ” {string1}” contains the substring  “{string4}” = {string2 in string1}’)
If string2 in string1:
   print(f’ ”{string1}” contains the substring “ ”{string2}” ‘)
else : 
   print(f’ ”{string1}” does not contain the substring “ ”{string2}” ‘)

Output :

“we all love python programming language” contains the substring “python”  = true
“we all love python programming language” contains the substring “like” = false
“we all love python programming language” contains the substring ”java” = false
“we all love python programming language” contains the substring “python”

Explanation of code :

Also check :   Best IDE for Angular development

string1 is the string variable that contains the first main string. string2,  string2, string3, and string4 are also a string variable that might contains a substring.

We want to check each of them whether it present in parent string1 or not.

And if you don’t know what fis in the print statement don’t worry it is normally a string formatting in python.

In the statement, string2 in string1 in is the operator that checks whether a substring belongs to string or not.

Using contains() function

Now let’s move on to another method that is used to check whether a string contains a substring or not.

_contains_() method is a very useful method for checking a substring belong to a string or not.basically the in operator internally calls the _contains_() method also.

So we will try it directly because it is also very easy.

Example code:

a  = ‘xyz’

print(‘a contains x  = ‘, a._contains_(‘x’))
print(‘a contains y  = ‘, a._contains_(‘y’))
print(‘a contains z  = ‘, a._contains_(‘z’))
print(‘a contains X  = ‘, a._contains_(X))

Output :

a contains x = true
a contains y = true 
a contains z = true 
a contains X = false

Explanation of the code :

a is a string type variable that contains a string ‘xyz’.we apply the _contains_() method in variable a and pass the substring that we want to check as the parameter in the function.

The _contains_() method returns true or false. The _contains_() method will return true if the string contains the substring and it will return false if the string does not contain the substring.

Using find() function

Now, let’s check the last and the third method in our list that checks whether a string contains the substring or not.

Also check :   I built a free forever feedback platform from scratch in Pandemic !

The find() function: if the string contains the substring it will return the starting index of the substring otherwise it will return the value -1.

Example code :

string1 = ‘we all love python programming language’
string2 = ‘python’
string3 = ‘like’
string4 = ’ java ’
Index = string1.find(string2)
If index != -1:
   print(f’ “{string1}” contains “{string2}” ‘)
 else :
   print(f’ “{string1}”does not contains “{string2}” ‘)
Index = string1.find(string3)
  If index != -1:
   print(f’ “{string1}” contains “{string3}” ‘)
else:
  print(f’ “{string1}”does not contains “{string3}” ‘)

Output :

 “we all love python programming language” contains “python”

“we all love python programming language” does not contain “like”

Explanation of code :

An index is a variable that contains the index of the substring.

And then we apply the find() function to the string1 that will return the index of the substring. So it does not have the value -1.

We check if the index does not contain the value -1 then print the actual scenario. Further with the help of this function, you print the index of substring also.

Conclusion on checking Python string in string:

Now we have discussed a lot of some important functions that are used to find the substring in python.

We know that python is very easy and contains a lot of built-in functionality that makes the programmer’s life is easy. Substring methods are very important to use in the program.

Get occasional tutorials & guides in your inbox. No spam ever.


Buy Me A Coffee

Leave a Comment