Python string contains

Python String Contains

Python String contains actually an instance in python to check if the string object contains a specific string or not. It tends to return the value as Boolean true or false. If the string is present, the method will return true, otherwise, it will return false.

How to Check Python String contains the word

a = 'Name'
print('a contains Name =', a.__contains__('Name'))
print('a contains Age =', a.__contains__('Age'))
print('a contains Class =', a.__contains__('Class'))

The output of this program will be:

('a contains Name =', True)
('a contains Age =', False)
('a contains Class =', False)

The method contains() in Python String contains being used for functions.

We can use contains() in python in 3 ways. This helps the user in understanding if there is a sub-string present inside a string or not. The methods are:

Python IN operator:

The IN operator will return the value as valid if the substring will be present inside the string object. If not, the IN operator will return false. The program below will illustrate the use of IN operator.

x_string="Welcome to Developer Helps"
substring_1="Helps"
substring_2="Name"
print("Check if "+x_string+" contains "+substring_1+":")
print(substring_1 in x_string)
print("Check if "+x_string+" contains "+substring_2+":")
print(substring_2 in x_string)

The output of the program will be:

Check if Welcome to Developer Helps contains Helps:
True
Check if Welcome to Developer Helps contains Name:
False

Python count Method:

This method helps in counting the number of times a substring occurs in the string. The function count will return 0 if the substring is not present in the string.  Understand the syntax of count() below to see the accurate use in python:

string.count(substring)

Python Find Method:

This method will also help in finding whether a substring is present in a string or not. If the substring is present, find() method will return the initial index of the substring. Otherwise, it will return -1 if not present in the string object. Understand the program below to see the accurate use of found in python:

x_string="Welcome to Developer Helps" 
substring_1="o" 
substring_2="Language" 
print("Check how many times "+x_string+" contains "+substring_1+":")
print(x_string.find(substring_1)) 
print("Check how many times "+x_string+" contains "+substring_2+":")
print(x_string.find(substring_2))

The output of the python program using the count method will be:

Check how many times Welcome to Developer Helps contains o:
4
Check how many times Welcome to Developer Helps contains Language:
-1

There are other ways also to check the patterns of string matching in python. They are the index method, regular expression method, etc. For example, python has a pre-method for using regular expressions which is β€˜re’. The re method has the function β€˜search’ to match a sub-string. The user can frequently use it in places where string matching is not that easy. For example, in the cases where the program is case sensitive. This helps in the complex calculations of strings and does not slow the speed of regular expression use cases.

The user can use another which is string.index(). It is to find the occurrence of the starting index of the string object. This method tends to throw a ValueError exception in case the substring is not found in the string object. This exception is then handled using the try-except-else block in python.

Ignore case

This ‘ignore case’ comes into play when a user wants to create a case-insensitive script. He can simply useΒ the lowerΒ function. This function casts all the characters in a string to lowercase.

Let us consider the example below and see how the ‘ignore case’ works with a python string contains:

my_string = "Welcome to developer helps"

if my_substring.lower() in my_string.lower():
    print(True)
else:
    print(False)

Output:

True

Python string contains substring from list

the user is able to see if a substring is present in a string by a lot of methods. some of the methods are listed below:

  • List comprehension: In this method, the user can check for the list as well as the string elements. If he finds a match and the value returned is true, then he can find one and false is not using the conditional statements.
  • Using any(): The any() function is the easiest way to find the substring inside a string efficiently. it simply checks for the match if the string matches with each element present in the list.

Let us see the python code below to understand this better:

test_string = "There are 3 tiger in the jungle"

test_list = ['lion', 'tiger', 'Cheetah']

print("This is the original string : " + test_string)

print("This is the original list : " + str(test_list))

res = any(ele in test_string for ele in test_list)

print("Does string contain any list element : " + str(res))

Output:

This is the original string : There are 3 tiger in the
This is the original list : ['lion', 'tiger', 'Cheetah']
Does string contain any list element : True

Leave a comment

Your email address will not be published.