
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Python 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
- C# 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?
- How to check if a substring is contained in another string in Python
- Python program to count number of substring present in string
- How to check if string or a substring of string ends with suffix in Python?
- Check if a String Contains a Substring in Linux
- Check if given words are present in a string
- How to check if a string contains a substring in Golang?
- Check if element is present in tuple in Python
- Python Program to check if a string starts with a substring using regex
- Check if the given characters is present in Golang String
- PHP to check substring in a string
- How to check if the string begins with specific substring in Java?
