
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if substring present in string in Python
In python data analysis we may come across a scenario to check if a given substring is part of a bigger string. We will achieve this through the following programs.
With find
The find function finds the first occurrence of the specified value. If the value is not found then it returns -1. We will apply this function to the given string and design a if clause to find out is substring is part of a string.
Example
Astring = "In cloud 9" Asub_str = "cloud" # Given string and substring print("Given string: ",Astring) print("Given substring: ",Asub_str) if (Astring.find(Asub_str) == -1): print("Substring is not a part of the string") else: print("Substring is part of the string") # Check Agian Asub_str = "19" print("Given substring: ",Asub_str) if (Astring.find(Asub_str) == -1): print("Substring is not a part of the string") else: print("Substring is part of the string")
Output
Running the above code gives us the following result −
Given string: In cloud 9 Given substring: cloud Substring is part of the string Given substring: 19 Substring is not a part of the string
With count
The method count() returns the number of elements with the specified value in a string or data collection in python. In the below program we will calculate the count of the substring and if it is greater than 0 we conclude that the substring is present in the bigger string.
Example
Astring = "In cloud 9" Asub_str = "cloud" # Given string and substring print("Given string: ",Astring) print("Given substring: ",Asub_str) if (Asub_str.count(Astring)>0): print("Substring is part of the string") else: print("Substring is not a part of the string") # Check Agian Asub_str = "19" print("Given substring: ",Asub_str) if (Asub_str.count(Astring)>0): print("Substring is a part of the string") else: print("Substring is not a part of the string")
Output
Running the above code gives us the following result −
Given string: In cloud 9 Given substring: cloud Substring is not a part of the string Given substring: 19 Substring is not a part of the string
- Related Questions & Answers
- Python Program to check if a substring is present in a given string.
- C# program to check if a Substring is present in a Given String
- Java program to check if a Substring is present in a given String
- How to check if string or a substring of string starts with substring in Python?
- Python program to count number of substring present in string
- Check if element is present in tuple in Python
- How to check if a substring is contained in another string in Python
- How to check if string or a substring of string ends with suffix in Python?
- How to check if a string contains a substring in Golang?
- Check if element is present in tuple of tuples in Python
- Python Program to check if a string starts with a substring using regex
- Java Program to Check if a string contains a substring
- How to check if the string ends with specific substring in Java?
- How to check if the string begins with specific substring in Java?
- Python – Check if any list element is present in Tuple