Python string contains

Python String Contains

Python String contains is 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 true 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"
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:
Check if Welcome to Developer Helps contains Name:

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:


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" 
print("Check how many times "+x_string+" contains "+substring_1+":")
print("Check how many times "+x_string+" contains "+substring_2+":")

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

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

There are other ways also to check the patterns of string matching in python. They are index method, regular expression method etc. For example, python has a pre-method for using regular expressions which is β€˜re’. The re method has a function β€˜search’ to match a sub string. The user can frequently use it in the 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 thrown a ValueError exception in case the sub-string is not found in the string object. This exception is then handled using try-except-else block in python.

Leave a comment

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