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" 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:
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 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 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.