Which method would you use to determine whether a certain substring is present in a string Python?

String manipulation is a common task in any programming language. Python provides two common ways to check if a string contains another string.

Python check if string contains another string

Python string supports in operator. So we can use it to check if a string is part of another string or not. The in operator syntax is:

sub in str

It returns True if “sub” string is part of “str”, otherwise it returns False. Let’s look at some examples of using in operator in Python.

str1 = 'I love Python Programming'

str2 = 'Python'

str3 = 'Java'

print(f'"{str1}" contains "{str2}" = {str2 in str1}')
print(f'"{str1}" contains "{str2.lower()}" = {str2.lower() in str1}')
print(f'"{str1}" contains "{str3}" = {str3 in str1}')

if str2 in str1:
    print(f'"{str1}" contains "{str2}"')
else:
    print(f'"{str1}" does not contain "{str2}"')

Output:

"I love Python Programming" contains "Python" = True
"I love Python Programming" contains "python" = False
"I love Python Programming" contains "Java" = False
"I love Python Programming" contains "Python"

Which method would you use to determine whether a certain substring is present in a string Python?

If you are not familiar with f-prefixed strings in Python, it’s a new way for string formatting introduced in Python 3.6. You can read more about it at f-strings in Python.

When we use in operator, internally it calls __contains__() function. We can use this function directly too, however it’s recommended to use in operator for readability purposes.

s = 'abc'

print('s contains a =', s.__contains__('a'))
print('s contains A =', s.__contains__('A'))
print('s contains X =', s.__contains__('X'))

Output:

s contains a = True
s contains A = False
s contains X = False

Using find() to check if a string contains another substring

We can also use string find() function to check if string contains a substring or not. This function returns the first index position where substring is found, else returns -1.

str1 = 'I love Python Programming'

str2 = 'Python'

str3 = 'Java'

index = str1.find(str2)
if index != -1:
    print(f'"{str1}" contains "{str2}"')
else:
    print(f'"{str1}" does not contain "{str2}"')

index = str1.find(str3)
if index != -1:
    print(f'"{str1}" contains "{str3}"')
else:
    print(f'"{str1}" does not contain "{str3}"')

Output:

"I love Python Programming" contains "Python"
"I love Python Programming" does not contain "Java"

Which method would you use to determine whether a certain substring is present in a string Python?

You can checkout complete python script and more Python examples from our GitHub Repository.

An easy way to check if a string contains a particular phrase is by using an if ... in statement. We can do this as follows:

if 'apples' in 'This string has apples':
    print('Apples in string')
else:
    print('Apples not in string')

Today we'll take a look at the various options you've got for checking if a string contains a substring. We'll start by exploring the use of if ... in statements, followed by using the find() function. Towards the end, there is also a section on employing regular expressions (regex) with re.search() to search strings.

The example above demonstrated a quick way to find a substring within another string using an if ... in statement. The statement will return True if the string does contain what we're looking for and False if not. See below for an extension of the example used previously:

strings = ['This string has apples', 'This string has oranges', 'This string has neither']

for s in strings:
    if 'apples' in s:
        print('Apples in string')
    else:
        print('Apples not in string')

Out:

Apples in string
Apples not in string
Apples not in string

The output displays that our if ... in statement looking for 'apples' only returned True for the first item in strings, which is correct.

It's worth mentioning that if ... in statements are case-sensitive. The line if 'apples' in string: wouldn't detect 'Apples'. One way of correcting this is by using the lower() method, which converts all string characters into lowercase.

We can utilize the lower() method with the change below:

strings = ['This string has apples', 'This string has oranges', 'This string has Apples']

for s in strings:
    if 'apples' in s.lower():
        print('Apples in string')
    else:
        print('Apples not in string')

Out:

Apples in string
Apples not in string
Apples in string

Alternatively, we could use the upper() function to search for 'APPLES' instead.

The if .. in approach has the fastest performance in most cases. It also has excellent readability, making it easy for other developers to understand what a script does.

Of the three options listed in this article, using if ... in is usually the best approach for seeing if a string contains a substring. Remember that the simplest solution is quite often the best one!

Another option you've got for searching a string is using the find() method. If the argument we provide find() exists in a string, then the function will return the start location index of the substring we're looking for. If not, then the function will return -1. The image below shows how string characters are assigned indexes:

Which method would you use to determine whether a certain substring is present in a string Python?

We can apply find() to the first if ... in example as follows:

strings = ['This string has apples', 'This string has oranges', 'This string has neither']

for s in strings:
    apples_index = s.find('apples')
    if apples_index < 0:
        print('Apples not in string')
    else:
        print(f'Apples in string starting at index {apples_index}')

Out:

Apples in string starting at index 16
Apples not in string
Apples not in string

For the first list item, 'apples' started at index 16, so find('apples') returns 16. 'apples' isn't in the string for the other two items, so find('apples') returns -1.

The index() function can be used similarly and will also return the starting index of its argument. The disadvantage of using index() is that it will throw ValueError: substring not found if Python can't find the argument. The find() and index() functions are also both case-sensitive.

Regex is short for regular expression, which is kind of like its own programming language. Through re.search, a regex search, we can determine if a string matches a pattern. The re.search() function generates a Match object if the pattern makes a match.

Here's an example:

import re

re.search('apples', 'This string has apples')

Out:

<re.Match object; span=(16, 22), match='apples'>

Looking at the Match object, span gives us the start and end index for 'apples'. Slicing the string using 'This string has apples'[16:22] returns the substring 'apples'. The match field shows us the part of the string that was a match, which can be helpful when searching for a range of possible substrings that meet the search conditions.

We can access the span and match attributes using the span() andgroup() methods, as follows:

print(re.search('apples', 'This string has apples').span())

print(re.search('apples', 'This string has apples').group())

If the substring isn't a match, we get the null value None instead of getting a Match object. See the example below for how we can apply regex to the string problem we've been using:

strings = ['This string has apples', 'This string has oranges', 'This string has neither']

for s in strings:
    if re.search('apples', s):
        print('Apples in string')
    else:
        print('Apples not in string')

Out:

Apples in string
Apples not in string
Apples not in string

In this case, the if statement determines if re.search() returns anything other than None.

We could argue that regex might be overkill for a simple functionality like this. But something like the example above is a great starting point for regex, which has plenty of other capabilities.

For instance, we could change the first argument of the search() function to 'apples|oranges', where | is the "OR" logical operator. In this context re.search() would return a match object for any strings with the substring 'apples' or 'oranges'.

The following demonstrates an example of this:

strings = ['This string has apples', 'This string has oranges', 'This string has neither']

for s in strings:
    if re.search('apples|oranges', s):
        print('Apples or oranges in string')
    else:
        print('Neither fruit is in string')

Out:

Apples or oranges in string
Apples or oranges in string
Neither fruit is in string

The easiest and most effective way to see if a string contains a substring is by using if ... in statements, which return True if the substring is detected. Alternatively, by using the find() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the substring. REGEX is also an option, with re.search() generating a Match object if Python finds the first argument within the second one.

How do you check if a string in Python contains a substring?

The easiest and most effective way to see if a string contains a substring is by using if ... in statements, which return True if the substring is detected. Alternatively, by using the find() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the substring.

Which method would you use to determine whether a certain substring is present in a string?

Which method would you use to determine whether a certain substring is present in a string? The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters.

Which method would you use to determine whether a substring is present in a string and returns the index of where the first occurrence is begins?

The indexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring.