Table of Contents
In this Python tutorial we will cover below topics
- check if a string contains another substring
- Match exact substring inside string
- case-insensitive match
“in” and “not in” operators
The operators in
and not in
test for membership in Python. This is the best and most used method to check if Python string contains another string. As it has been identified that this operator has the least performance impact compared to other methods which I will show you in this tutorial. These operators would return boolean expression i.e. either True
or False
""
in "abc"
will return True
.#!/usr/bin/env python3 string = 'Entertainment' substring = 'ent' # returns boolean value print(substring in string) # based on the return value, the condition statement would be executed if substring in string: print('Found') else: print('Not Found')
Output from this script:
# python3 /tmp/check_string.py
True
Found
Similarly we can verify the use case for "not in
" operator. The operator not in
is defined to have the inverse truth value of in
.
#!/usr/bin/env python3 string = 'Entertainment' substring = 'ent' # returns boolean value print(substring not in string) # based on the return value, the condition statement would be executed if substring not in string: print('Not Found') else: print('Found')
Output from this script:
# python3 /tmp/check_string.py
False
Found
case-insensitive match
To perform case-insensitive match of substrings within a string in Python can be achieved using a couple of methods.
Method 1: Using upper() or lower()
We can use str.upper()
or str.lower()
to convert the case of the string and then perform the match between strings and sub strings using "in
" or "not in
" operator
#!/usr/bin/env python3 string = 'Entertainment' substring = 'ENT' # returns boolean value print(substring.lower() in string.lower()) # based on the return value, the condition statement would be executed if substring.lower() in string.lower(): print('Found') else: print('Not Found')
Output from this script:
# python3 /tmp/check_string.py
True
Found
Method 2: Using regex search
Using regex
gives you more flexibility to search for a string. re.search
will scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. It will return "None
" if no position in the string matches the pattern
The syntax would be:
re.search(pattern, string, flags=re.IGNORECASE)
We will use this regex search in our sample script
#!/usr/bin/env python3 import re string = 'Entertainment' substring = 'ENT' # returns match object print(re.search(substring, string, flags=re.IGNORECASE)) # based on the return value, the condition statement would be executed if re.search(substring, string, flags=re.IGNORECASE): print('Found') else: print('Not Found')
Output from this script:
# python3 /tmp/check_string.py
<_sre.SRE_Match object; span=(0, 3), match='Ent'>
Found
Check for substrings in strings using str.index()
str.index()
can be used to lookup for the index
value of the first match of a pattern or substring inside a string. This will return the starting index number of the first occurrence of the match (if found) or it will raise a "ValueError
" exception
The syntax would be:
str.index(sub[, start[, end]])
Here you can define the starting and ending index
number to search for a substring inside a string
#!/usr/bin/env python3 string = 'Entertainment' substring = 'ent' # search for substring inside string print(string.index(substring)) # search for substring after index no 5 and before the last available index no print(string.index(substring, 5, len(string)))
Output from this script:
# python3 /tmp/check_string.py
10
10
If the match is Not Found then we get valueError
exception:
# python3 /tmp/check_string.py
Traceback (most recent call last):
File "/tmp/check_string.py", line 7, in <module>
print(string.index(substring))
ValueError: substring not found
We can suppress this using try except else
block:
#!/usr/bin/env python3 string = 'Entertainment' substring = 'ENT' # a = full string and b = substring def check_string(a, b): try: # check if b substring is present in a string a.index(b) # Raise exception for ValueError and instead print False except ValueError: print('False') else: print('True') check_string(string, substring)
Output from this script:
# python3 /tmp/check_string.py
False
Check for substring in string using str.find()
You can choose str.find
over str.index
as here we don't have to worry about handling exceptions. This method also returns the lowest index in the string where substring sub is found but if a pattern or subtring is not found then this will return "-1
"
The syntax would be:
str.find(sub[, start[, end]])
Below sample python script would return the index value when the first occurrence of substring is found in string
#!/usr/bin/env python3 string = 'Entertainment' substring = 'ent' # returns index value if match found and -1 if no match print('Value: ', string.find(substring))
Output from this script:
# python3 /tmp/check_string.py
Value: 10
If there is No Match
found, then the output would be:
# python3 /tmp/check_string.py
Value: -1
find()
method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in
or not in
operator
Use regular expressions (re.search)
We used re.search
earlier in this tutorial to perform case insensitive check for substring in a string. We can use the same method for case sensitive match without using flags = re.IGNORECASE
The re
module is not an inbuilt function so we must import this module. Either we can import all the contents of re
module or we can only import search
from re
#!/usr/bin/env python3 import re string = 'Entertainment' substring = 'ent' # returns match object print(re.search(substring, string)) # based on the return value, the condition statement would be executed if re.search(substring, string): print('Found') else: print('Not Found')
Output from this script:
# python3 /tmp/check_string.py
<_sre.SRE_Match object; span=(10, 13), match='ent'>
Found
If there is No match then re.search
would return None
and the output would be:
# python3 /tmp/check_string.py
None
Not Found
Match exact substring inside string
The above methods only checks for a sub inside string but that is not an exact match. For example abc
will match abcd
, dabc
and abc
. So if your requirement is to match for exact string i.e. abc
then we must use word boundaries with re.search
.
What are word boundaries “\b” ?
\b
matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of word characters. Note that formally, \b
is defined as the boundary between a \w
and a \W
character (or vice versa), or between \w
and the beginning/end of the string. This means that r'\bfoo\b'
matches 'foo'
, 'foo.'
, '(foo)'
, 'bar foo baz'
but not 'foobar'
or 'foo3'
.
The syntax to be used would be:
re.search(r'\b'+substring+r'\b',string)
If you are using a variable with re.search
then you must escape the substring using:
re.search(r'\b'+re.escape(substring)+r'\b',string)
Let us use this in our example python script:
#!/usr/bin/env python3 import re string = 'abc abcd def' substring = 'abc' # returns match object if found or returns None if not found print(re.search(r'\b'+re.escape(substring)+r'\b', string))
The output from this script:
# python3 /tmp/check_string.py
<_sre.SRE_Match object; span=(0, 3), match='abc'>
But the substring is not matched then the output would be None
# python3 /tmp/check_string.py
None
Conclusion
In this tutorial we learned about different methods available in Python to check if a string contains substring. Each of these methods have their own pros and cons so based on your requirement you may choose the appropriate function. In most cases the in operator should suffice the requirement using "in
" operator.
Lastly I hope this article on Python programming was helpful. So, let me know your suggestions and feedback using the comment section.