Python : Check if a String contains a Sub String | Case Insensitive

Strings are one of the most commonly used types in Python. We can easily make them by enclosing characters in quotes. Python considers single quotes to be the same as double quotes. String creation is as easy as assigning a value to a variable.

Let’s look at different ways to solve the general problem of determining whether a specific piece of string is present in a larger string. This is a very common type of problem that every programmer encounters at least once in his or her career. This article discusses various methods for resolving it.

Given a string and substring ,the task is to check if the substring is present in given string.

Examples:

1)Case sensitive

Input:

string = 'This is BTechGeeks online platform'  substring = 'online'

Output:

Yes

2)Case insensitive

Input:

string = 'This is BTechGeeks Online platform'  substring = 'online'

Output:

Yes

Determine whether a String contains a Sub String

There are several ways to check whether a string contains given substring some of them are:

Method #1:Using in operator

The in operator is the most generic and fastest way to check for a substring in Python. The power of the in operator in Python is well known and is used in many operations throughout the language.

1)Case sensitive

Below is the implementation:

# given string
string = 'This is BTechGeeks online platform'
# given substring
substring = 'online'
# checking if the substring is present in string
if substring in string:
    print('Yes')
else:
    print('No')

Output:

Yes

2)Case Insensitive

We can check them by converting both given string and substring to lower case or uppercase.

Below is the implementation:

# given string
string = 'This is BTechGeeks Online platform'
# given substring
substring = 'online'
# checking if the substring is present in string by converting to lowercase
if substring.lower() in string.lower():
    print('Yes')
else:
    print('No')

Output:

Yes

Method #2: Using not in operator

Similarly, we can use the “not in” operator to test the opposite scenario, that is, to see if a string or character does not exist in another string.

1)Case sensitive

Below is the implementation:

# given string
string = 'This is BTechGeeks online platform'
# given substring
substring = 'online'
# checking if the substring is present in string
if substring not in string:
    print('No')
else:
    print('Yes')

Output:

Yes

2)Case insensitive

We can check them by converting both given string and substring to lower case or uppercase.

Below is the implementation:

# given string
string = 'This is BTechGeeks online platform'
# given substring
substring = 'online'
# checking if the substring is present in string by converting to lowercase
if substring.lower() not in string.lower():
    print('No')
else:
    print('Yes')

Output:

Yes

Method #3:Using str.find()

The str.find() method is generally used to get the lowest index at which the string occurs, but it also returns -1 if the string is not present; thus, if any value returns greater than zero, the string is present; otherwise, it is not present.

1)Case sensitive

Below is the implementation:

# given string
string = 'This is BTechGeeks online platform'
# given substring
substring = 'online'
# using find operator
result = string.find(substring)
# if result is greater than 0 then print yes
if result > 0:
    print('Yes')
else:
    print('No')

Output:

Yes

2)Case insensitive

We can check them by converting both given string and substring to lower case or uppercase.

Below is the implementation:

# given string to lower
string = 'This is BTechGeeks Online platform'.lower()
# given substring to lower
substring = 'online'.lower()
# using find operator
result = string.find(substring)
# if result is greater than 0 then print yes
if result > 0:
    print('Yes')
else:
    print('No')

Output:

Yes

Method #4:Using index() function

This method can be used to perform the same task as str.find(), but unlike str.find(), it does not return a value, but rather a ValueError if string is not present, so catching the exception is the only way to check for string in substring.

1)Case sensitive

Below is the implementation:

# given string
string = 'This is BTechGeeks Online platform'
# given substring
substring = 'online'
# using try and except
# using index function
try:
    result = string.find(substring)
    print('Yes')
except:
    print('No')

Output:

Yes

2)Case insensitive

We can check them by converting both given string and substring to lower case or uppercase.

Below is the implementation:

# given string to lower
string = 'This is BTechGeeks Online platform'.lower()
# given substring to lower
substring = 'online'.lower()
# using try and except
# using index function
try:
    result = string.find(substring)
    print('Yes')
except:
    print('No')

Output:

Yes

Leave a Comment