Python | Check if a substring presents in a string

By IncludeHelp Last updated : February 13, 2024

Problem statement

Given a string, write a Python program to check if a substring presents in a string.

Consider the below-given examples -

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.

Checking if a substring presents in a string

In Python, you can use the 'in' operator to check if a substring presents in a string or not. The 'in' operator checks whether a substring is in the string or not, and returns True if the substring present in the string; False, otherwise.

Python program to check if a substring presents in a string

Here is the Python code to check whether a substring is present in a string or not:

# 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

The output of the above program is:

Yes, substring presents in the string. 
No, substring does not present in the string.	

Python String Programs »

To understand the above program, you should have the basic knowledge of the following Python topics:

Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.