How To Check If A Python String Contains A Substring?

Asked 3 months ago
Answer 1
Viewed 104
0

Checking whether a string contains substrings allows you to aggregate conditions and create more flexible code. Additionally, depending on the domain model, when you check whether a string contains a substring, you can also set a field from an object when the string itself encodes a field.

In operator

The easiest way to check if a Python string contains substrings is to use the inside operator.
In operator is used to check the membership of a Python data structure. Returns a boolean value (true or false). In Python, to check if a string contains a substring with the in operator, we simply call it a superstring:

 // Your code goes herefullstring = "StackAbuse" substring = "tack" if substring in fullstring: print("Found!") else: print("Not found!")

This operator is a shortcut for calling the __contains__ method of an object, and is also useful for checking whether an element is in a list. It should be noted that it is not scratch resistant. So if our entire string is set to None, it will throw an exception:

TypeError: argument of type 'NoneType' is not iterable

To avoid this, you need to do the following first: I want to check if Shown or not shown:

fullstring = None
substring = "tack"
if fullstring != None and substring in fullstring:
  print("Found!")
else:
  print("Not found!")

String.index() Method

In Python, the string type has a method called index() that can be used to find the starting index of the first occurrence of the substring in the character string.

If the substring is not found, a ValueError is raised and an exception is thrown that can be handled with a try-exclusive-else block: 

fullstring = "StackAbuse" 
substring = "tack" 
try: 
   fullstring.index(substring) 
except ValueError: 
   print("Not found!") 
else: 
   print("Found!")

This method is useful when you need to know the position of a substring, not just its existence in the entire string. The method itself returns the index:

print(fullstring.index(substring)) 

However, to check if a string contains a substring, this is a verbose method

String.find() Method

The String class has another method called find(), which is easier to use than index() because you don't have to worry about exception handling.

If find() doesn't find a match, it returns -1; otherwise, the leftmost part is returned. Indexes of substrings within a larger string:

fullstring = "StackAbuse" 
substring = "tack" 
if fullstring.find(substring) != -1: 
   print("Found!") 
else: 
   print("Not found!")

Obviously performs the same search as index() and returns the starting index of the substring of the main string:

 // Your code goes here
print(fullstring.find(substring)) 

Regular Expressions (RegEx)

Regular expressions provide a more flexible (though more complex) way to check whether a string matches a pattern. Regular expressions allow flexible and efficient searching of much larger search fields than a simple check as described above.

Python uses the input module. for regular expressions that are called back. The Re module has a function called search() that allows you to search for sub-string patterns:

from re import search 
fullstring = "StackAbuse" 
substring = "tack" 
if search(substring, fullstring): 
    print "Found!" 
else: 
    print "Not found!"

This method is best when you need more complex matching features, such as case sensitivity or when working with large search spaces. Otherwise, a simple substring match should avoid the complications and slower speed of regular expressions.

Read Also : What can I do with excess guitar strings?
Answered 3 months ago Berglunds  snabbköpBerglunds snabbköp