How do I check if a string contains something in Python?

In this article, we are going to discuss python string contains substring, integer, character. What does the python string contains? Why do we want to know what a python string contains? How could you know …

Python string contains

In this article, we are going to discuss python string contains substring, integer, character.

  • What does the python string contains?
  • Why do we want to know what a python string contains?
  • How could you know if a python string contains a substring or not?

What does the python string contains?

Python string contains refers to what is included in a python string.

It could be any substring, integer value, character, the regular expression could be stated as python string contains substring, python string contains an integer, python string contains character, python string contains regex (regular expression).

Why do we want to know what python string contains?

We need to know what python string contains because it could create trouble while executing the conditional statements; it is better to understand it.

Python string contains sub-string

How could we know python string contains sub-string?

There are few methods that you can follow to know if python contains a sub-string or not.

Methods are given below:

  • String contains
  • In-operator
  • Find 
  • Index.

String contains

The string contains a built-in method in python to know whether the python string contains another string or not. 

It will return the Boolean value as output when the string contains substring; then, it will display true. Otherwise, it will display false.

Let’s understand it with the help of an example:

Example:

#python string contains method
#python string contains sub-string

my_str = "Hello Drastic code"
 
substring = "code"
 
if (my_str.__contains__(substring)):
    print("String contains sub-string !")
else:
    print("String does not contain sub-string")

Output:

String contains sub-string !

In the above example, we first defined the main string.

Then, sub-stringAfter

that, we used the if-else conditions and string contains method to search for a substring.

In- operator:

In-operator is used in the string method to know if the python string contains any sub-string or not.

It will return the results in Boolean form means in (True or False ), and it is a case-sensitive means if you have searched the sub-string in upper-case and it is in lower-case in string then, it will return false.

Let’s understand it with the help of an example:

Example:

#python string contains sub-string
#in-operator

str="Drastic code is a tech-web"
sub1="code"
sub2="technical"
 
print(sub1 in str) 
 
print(sub2 in str)

Output:

True
False

In the above example, 

First, we defined the string, 

Then, two sub-strings: sub1 and sub2

after that, used in-operator within the print statement.

Find

The find method is used to search if the python string contains substring; the find method will return the output in integer form. 

The integer usually returns the indexes of the string if the string exists; otherwise, it will return -1.

Let’s understand it with the help of an example:

Example:

#python string contains sub-string 
#find method
#python string

str="Drastic code is a tech-web"
if str.find("tech")!=-1:
  print("Found the string")
else:
  print("Not found!!!")

Output:

Found the string

In the above example,

First, we defined the string 

Then,  used if-else conditions and find method.

Index method:

The index method is used to check if the python string contains substring or not. If it consists then, it will print the starting index of the sub-string as an output; otherwise, it will display ValueError.

Let’s take an example to understand it better:

Example:

#python string contains
#index method

str = "Drastic code is tech-web."
try :  
    result = str.index("web") 
    print ("web is present in the string.") 
except : 
    print ("web is not present in the string.")

Output:

web is present in the string.

In the above example, 

First, we defined the string

Then, used try-except blockAnd

within the try-except block, we used the str.index method to know whether the string contains substring or not.

Python string contains an integer.

Python string contains an integer, which is used to know whether the python string consists of any integer or any digit(0-9).

For this, we will use the isdigit method:

Let’s understand it with the help of an example:

Example:

#python string contains integer
#isdigit method

contains_digit = False
s = "Drastic code 1"
for character in s:

    if character.isdigit():
        
        contains_digit = True
        print(contains_digit)

Output:

True

In the above example, 

first, we stated the condition and set it to false.

Then, we defined the string which contains an integer

Next, we used for loop and if condition.

Within the if-condition, we used the isdigit method.

Then, again stated the condition and set it to True.

In the end, we used the print function to print the output, whether it is true or false.

Python string contains characters.

Python string contains characters used to check whether the string contains characters or not.

It uses the same methods as python string contains sub-string to check whether python string contains characters.

Let’s take an example of an in-operator:

Example:

#python string contains character 
#in-operator

str="Drastic code is a tech-web"
char1="c"

print(char1 in str)

Output:

True

In the above example, 

First, we defined the string

Then, char1 define a character 

Next, we used an in-operator within the print statement to check whether the python string contains characters or not.

Python string contains regex (regular expression)

As regex stands for regular expression, in python, we use regex to search for matching patterns smoothly. Python contains build-in module re. The re module has a search function that can be used to check substring in a string.

Let’s take an example to understand it better:

Example:

#python string contains
#regular expression
#search function

#importing re module
from re import search
fullstring = "Drastic code"
substring = "stic"
if search(substring, fullstring):
    print("Exist!")
else:
    print("Not Exist!")

Output:

Exist!

In the above example, first, we imported the re module

Then, defined the string

Then, defined the sub-string

Next, used the search function with if-condition

Then, use the print function to display the condition 

Then, used else-condition and print function to display output.

Conclusion:

In this article, we have briefly discussed python string contains: python string contains sub-string, python string contains an integer, python string contains characters, python string contains regex.

Suggested Read: Remove Brackets from list Python

Leave a Comment