Home »
Python »
Python programs
Python | Check if a substring presents in a string using 'in' operator
An example of 'in' operator python: Here, we are going to learn how to check whether a substring presents in a string or not?
Submitted by IncludeHelp, on November 26, 2018
"in" operator is well known and most useful operator in python programming language, it can be used to check whether a substring is present in a string or not?
Example 1:
Input:
String: "IncludeHelp.com"
Substring to check: "Help"
Output:
Yes, substring presents in the string.
Example 2:
String: "IncludeHelp.com"
Substring to check: "Hello"
Output:
No, substring does not present in the string.
Program:
# python program to check substring
# presents in the string or not
# string and substring declaration, initialization
str = "IncludeHelp.Com"
sub_str ="Help"
# checking sub_str presents in str or not
if sub_str in str:
print("Yes, substring presents in the string.")
else:
print("No, substring does not present in the string.");
# testing another substring
sub_str = "Hello"
# checking sub_str presents in str or not
if sub_str in str:
print("Yes, substring presents in the string.")
else:
print("No, substring does not present in the string.");
Output
Yes, substring presents in the string.
No, substring does not present in the string.
TOP Interview Coding Problems/Challenges