Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

python

Python – Check if String Contains Substring

In this tutorial, we are going to see different ways to check if a string contains substring and if it does, look for its index. Also, we will check the existence of a substring ignoring the case.
 

Check if String Contains Substring Using ‘IN’ operator

The “in” operator in Python can be used to check if a given string exists in another string. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
str = "Welcome to StackHowTo."
# Check if String Contains Substring
if "StackHowTo" in str:
print ('Substring found')
else:
print('Substring not found')
str = "Welcome to StackHowTo." # Check if String Contains Substring if "StackHowTo" in str: print ('Substring found') else: print('Substring not found')
str = "Welcome to StackHowTo."
# Check if String Contains Substring
if "StackHowTo" in str:
    print ('Substring found')
else:
    print('Substring not found')

Output:

Substring found
[st_adsense]  

Check if String Contains Substring Using ‘NOT IN’ operator

We can also use the ‘NOT IN’ operator to check the opposite scenario. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
str = "Welcome to StackHowTo."
# Check if String Contains Substring
if "StackHowTo" not in str:
print('Substring not found')
else:
print ('Substring found')
str = "Welcome to StackHowTo." # Check if String Contains Substring if "StackHowTo" not in str: print('Substring not found') else: print ('Substring found')
str = "Welcome to StackHowTo."
# Check if String Contains Substring
if "StackHowTo" not in str:
    print('Substring not found')
else:
    print ('Substring found')

Output:

Substring found

 

Check if String Contains Substring in a Case-insensitive Way

To check whether a given string contains a substring in a case-insensitive way, i.e. ignoring case, we must first transform both strings to lowercase, and then use the “in” or “not in” operator to check if substring exist. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
str = "Welcome to StackHowTo."
# Convert both strings to lowercase
if "STACKHOWTO".lower() in str.lower():
print ('Substring found')
else:
print('Substring not found')
str = "Welcome to StackHowTo." # Convert both strings to lowercase if "STACKHOWTO".lower() in str.lower(): print ('Substring found') else: print('Substring not found')
str = "Welcome to StackHowTo."
# Convert both strings to lowercase
if "STACKHOWTO".lower() in str.lower():
    print ('Substring found')
else:
    print('Substring not found')

Output:

Substring found
[st_adsense]  

Check if String Contains Substring Using Regex

We can also use python’s regex “re” module to check if a given string exists within another string. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import re
str = "Welcome to StackHowTo."
# Create a pattern to match the string 'StackHowTo
motif = re.compile("StackHowTo")
# searches for the pattern in the string and returns the corresponding object
obj = motif.search(str)
# check if the object is not Null
if obj:
print ('Substring found')
else:
print('Substring not found')
import re str = "Welcome to StackHowTo." # Create a pattern to match the string 'StackHowTo motif = re.compile("StackHowTo") # searches for the pattern in the string and returns the corresponding object obj = motif.search(str) # check if the object is not Null if obj: print ('Substring found') else: print('Substring not found')
import re

str = "Welcome to StackHowTo."
# Create a pattern to match the string 'StackHowTo
motif = re.compile("StackHowTo")
# searches for the pattern in the string and returns the corresponding object
obj = motif.search(str)
# check if the object is not Null
if obj:
    print ('Substring found')
else:
    print('Substring not found')

Output:

Substring found
[st_adsense] mcq

Leave a Reply

Your email address will not be published. Required fields are marked *