Computer >> Computer tutorials >  >> Programming >> Python

Python Program to check if a substring is present in a given string.


In this problem a string is given, we have to check if a substring is present in the given string.

Algorithm

Step 1: input a string and a substring from the user and store it in separate variables.
Step 2. Check if the substring is present in the string or not. To do this using find() in-built function.
Step 3. Print the final result.
Step 4. Exit.

Example Code

def check(str1, sstr): 
   if (str1.find(sstr) == -1): 
      print(sstr,"IS NOT PRESENT IN THE GIVEN STRING") 
   else: 
      print(sstr,"IS PRESENT IN THE GIVEN STRING") 
# Driver code 
str1 = input("Enter the string ::>")
sstr=input("Enter Substring ::>")
check(str1, sstr) 

Output

Enter the string ::> python program
Enter Substring ::> program
program IS PRESENT IN THE GIVEN STRING
Enter the string ::> python program
Enter Substring ::> programming
programming IS NOT PRESENT IN THE GIVEN STRING