Python How to check string for substring

In python you can test a string if it contains another string by many ways. In this post I'll try to cover some of the popular ones:

  • using in syntax
  • using find function
  • using rfind function or find vs rfind - to search from right to left
  • using method index - raising error if the string is not found
  • using regular expressions - for more advanced searches

using in syntax

The most simple and trivial solution of this problem is to use syntax:

substring in testedstring
if "string" in "Python How to check string for substring":
    print('found string')
if "String" in "Python How to check string for substring":
    print('found String')

result:

found string

Pay attention that this search ( and most of the other) are case sensitive. This means that searching for string and String are two different strings.

You can create simple case insensitive search by:

if "String".lower() in "Python How to check string for substring".lower() :
    print('found String')

result:

found string

using find function

Another way to see if a string contains another string of substring is by using method find. This one is case insensitive:

if "Python How to check string for substring".find("check"):
    print('found check')
if "Python How to check string for substring".find("Check"):
    print('found Check')

result:

found check
found Check

using rfind function or find vs rfind

This function is used when you have more than one occurrence and your want to search them from right to left. The example below shows the difference between find and rfind:

print("Python How to check string for substring".rfind("in"))
print("Python How to check string for substr".rfind("in"))
print("Python How to check stri1ng for substring".find("in"))
print("Python How to check string for substring".find("in"))

result:

37
23
38
23

So find and rfind return the index of the first found element. In this example we search for in. In some of the cases in is removed or changed.

using method index

Alternative way of searching is by using method index. This one will return the index of start string in the other string. This is the method prototype:

 string.index(s, sub[, start[, end]])

Start and end are optional arguments providing information where do you want to search. This example below show several ways to search string in another string with method index:

print("Python How to check string for substring".index("in"))
print("Python How to check string for substring".index("in", 30))
print("Python How to check string for substring".index("in", 25, 40))

The difference with the find method is that this one is going to raise error:

ValueError: substring not found

using regular expressions

If you have more general requirements for your search like:

  • find a date - any date, not specific one
  • find a name - any name, a word starting with capital letter
  • find a abbreviation

then you can use regular expressions or regex to find whether a string contains another string in python

import re
print (re.search("Python How to check string for substring", "check"))
print (re.search("How", "Python How to check string for substring"))

result:

None
<_sre.SRE_Match object; span=(7, 10), match='How'>

As you can see if the string doesn't contains the string we get result None. ( we are searching for Python how to ... is it in string check ).
And if the string contains the other string you get information about the match and the start and end of the match - in other words where the string is in the other string.

Resources