HomePythonPython Program to Check if a Substring is Present in a Given String

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

String manipulation is a fundamental skill in programming, and one common task is to determine whether a substring exists within a given string. This program demonstrates how to create a Python program to accomplish this task efficiently using built-in string methods.

Problem Statement

Write a Python program that takes a main string and a substring as input and checks whether the given substring is present within the main string.

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

def is_substring_present(main_string, substring):
    if substring in main_string:
        return True
    else:
        return False

# Input
main_string = input("Enter the main string: ")
substring = input("Enter the substring to check: ")

# Checking for substring presence
if is_substring_present(main_string, substring):
    print("Substring is present in the main string.")
else:
    print("Substring is not present in the main string.")

Input / Output

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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...