To Begin with Python String Includes
In this article, we’ll look at the _contains_() function of the String class in Python, which checks whether the supplied substring is contained in the given string and returns true if it is there else returns false. series are also available in Python using the pandas module. You may use the str.contains () method to see whether a particular pattern appears in a given string of series, and the function will return a Boolean value (true) if the pattern appears in the series, and a false value otherwise. In Python, you can also use the regular expression, “in” operator, find(), etc. to determine whether a string is contained in another string.
Python’s string _contains_() Function: How It Works
In this article, we’ll look at the _contains_() function provided by the string class to determine whether a particular substring is included in the string. Similar to the _contains_() method of the string class, the contains() method can be found in another module called “operator” in Python. You may also get the sequence of functions by utilising the pandas library’s string search feature. Use str.contains() to see whether a certain pattern appears in the series string.
Let’s look at some real-world applications of the aforementioned features:
The String. contains_() built-in method in Python may be used to locate a substring inside the supplied string. Syntax:
str._contains_(substring)
In order to compare two strings, the _contains_() method is used. The preceding syntax reveals that both “str” and “substring” are string objects, with “str” containing one string and “substring” containing another. The _contains_() method checks whether the string object “str” in the syntax includes the string (substring) that is supplied to it as an input. This function is case-sensitive, so be careful.
This is a simple String Contains example in Python.
The many uses of Python String Contains are shown below. For the sake of Illustration:
Here is the output of the print("Python program to demonstrate _contains_() function")
:
print("\n")
str1 = "Educba Training"
sub_str = "Educba"
if (str1.__contains__(sub_str)):
print("The given String contains the given substring")
print(sub_str)
print("\n")
else:
print("The given String does not contain substring")
Notice how “str1” is set to “Educba Training” and “sub str” is set to “Educba” in the code above. The _contains_() function checks whether a given string includes a specified substring; if it does, it returns a true value, and the corresponding “if” statement outputs the substring.
This _contains_() string function may also be implemented as a class method by passing two arguments to the str.method of the String class. Syntax:
o = str_cl._contains_(s1, s2)
This method of the string class is called with two strings, s1 and s2, as arguments. If s1 contains s2, then the function returns true; otherwise, it returns false. Example: Code:
Input: print(str.__contains__("Educba Training Institute", "Institute"))
Result:
Let’s have a look at the following example to show how the “operator” module of Python offers the contains() method, which is functionally equivalent to the string function _contains_(). Example: Code:
The Result of the import operator
is:
print("Python program to demonstrate contains() function of operator module ")
print("\n")
str1 = "Educba is best in programming courses."
if operator.contains(str1, "best"):
print ("The given substring is present in the string.")
else :
print ("The given substring is not present in the string.")
We can see that in the code above, one string (called “str1”) is defined and a substring is given as an input to a function. In order for this code to work, we need to import the “operator” module, and then we can use the operator.contains() function, which searches for the substring specified by the second argument, str2, in the string specified by the first argument, str1. If the function returns true, the substring is printed; otherwise, it returns false. Above you can see the results.
We’ll look at the contains() method in pandas, which is used to identify a match in a string of series data.
str.contains().
Let’s see the syntax of this function in action in the following programme. Syntax:
Series.str.contains(pat, case=True, flags=0, na=nan, regex=True)
Example of a Code
-
pat:
Parameter to specify the characters or substring or regular expression. -
case:
True, this parameter is set to true as to say this function is case sensitive. -
flag:
It is used for going through the re module. -
na:
Which is always none, to fill values for missing values. -
regex:
This parameter is set to true if the pat parameter is regular expression else it is set to false as pat is a literal string.
Here is the output of the import pandas as pd
:
print("Program to demonstrate contains() function provided by pandas")
print("\n")
sr = pd.Series(['Educba', 'Training', 'Institue', 'Chennai', 'India'])
print("The given series along with its index and type is as follows:")
print(sr)
print("\n")
r = sr.str.contains('Edu')
print(r)
If we want to utilise the pandas function series in the above code, we must first import pandas.
str.contains(). Using the pandas object pd, the above code generates a series whose elements are identified by their indexes in the output. In such case, we use a series. Using str.contains() with a search substring of “Edu,” we’ll look for that substring in the series string “sr” that was just constructed and defined. If the specified substring is present in one of the strings in the series, the function will return true; otherwise, it will return false, and the corresponding Boolean value for that string will be printed along with the index number.