Python Program to Check if the Substring is Present in the Given String

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

Problem Description

The program takes a string and checks if a substring is present in the given string.

Problem Solution

1. Take a string and a substring from the user and store it in separate variables.
2. Check if the substring is present in the string using find() in-built function.
3. Print the final result.
4. Exit.

Program/Source Code

Here is source code of the Python Program to calculate the length of a string without using library functions. The program output is also shown below.

string=raw_input("Enter string:")
sub_str=raw_input("Enter word:")
if(string.find(sub_str)==-1):
      print("Substring not found in string!")
else:
      print("Substring in string!")
Program Explanation

1. User must enter a string and a substring from the user and store it in separate variables.
2. It is then checked if the substring is present in the string using find() in-built function.
3. An if statement is used to make the decision and the final result is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter string:Hello world
Enter word:world
Substring in string!
 
Case 2:
Enter string:Hello world
Enter word:apple
Substring not found in string!

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.