So do you want to know how to check the presence of a substring in a string in Python? Then this is the right article for you! In this article, I will explain you how to write simple Python code to see if a substring exists within a string. Does that sound good to you? Great then let us get started!
But before we take a look at the code, let us explore where we often come across this kind of requirement. Where do you think you want to check the presence of a substring within a string? Any guess?
Python Check Substring In A String – Use Case
Well, the first thing that comes to my mind is during data extraction from a data dump. Say for example, you have been given a data dump of customer emails. Your manager wants you to go through the dump and check to see how often people complain about a particular problem. Say for example, customers always complain about battery low error message that pops up on their smartphone screen reads like: “Error: Battery Low!” when using your product.
So how do you go about solving this? One crude method is to go through each of the customer emails and count the emails that talks about this error message. This will give you statistical data on that particular error. But do you think this is efficient?
No! Because you being a Python programmer should not be doing this by hand. Instead, what I would say is this:
Write a Python script to check for the substring in a string. So then you can go through each email text’s strings and check for the substring:
“Error: Battery Low!”
and count how many emails has this error message. Now that is efficient. Right?!
But this is not the only time you may want to write a Python script for substring check. Because you can use this similar concept when working with databases as well! So if you are developing a website using Flask or Django, similar code will come in handy. Say you want to fetch all customers from the DB whose name contains “Jeff” in them. You can use the same code logic even there. Right?
So, how does the code look like then?
Python Script To Check Substring In String
Here is how you can write code to check substring in a string:
import sys def extract_strings_with(sub_string, strings_list): for string in strings_list: if sub_string in string.lower(): print(string) if __name__ == "__main__": names = ["AbsoulteDelight", "Abnormal", "Alltools", "Activator", "Alladdin", "Absoulte Vodka"] if len(sys.argv) < 2: print("Usage: substring_check.py <sub_name_to_check>\n \ Example: substring_check.py absolute") sys.exit(0) check_subname = sys.argv extract_strings_with(check_subname, names)
We run this Python script using the command:
python3 substring_check.py absolute
So as you can see from the above code, we are checking for the substring “absolute” present within a list of names.
So where is the magic of substring check within string happening? It is in the line:
if sub_string in string.lower():
Here, we are checking for the presence of the substring within the string element. We are using lower() function to ensure that this program is not case sensitive. But if you want it to be case sensitive, you should remove lower() function call in the above line of code.
So this is how you can check for the presence of a substring within a string using Python. But if you are still not clear, do let me know in the comments below. I will be more than happy to help!
So until next time, take care!