Python string contains 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 a given string.
- Python Program to Check if a Substring is Present in a Given String
- Python String contains() Method with Examples
- Python Program to Check if a String Contains at least One Number
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
Python find case insensitive: 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
Related Programs:
- check if type of a variable is string in python
- python check if a list contains all the elements of another list
- python program to check if a string is palindrome or not
- python program to check if a string is a pangram or not
- find all occurrences of a sub string in a string cpp both case sensitive insensitive
- python program to check if a substring is present in a given string
- python check if a value exists in the dictionary