Saturday, 4 January 2025

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

 


def check_substring(main_string, substring):

    if substring in main_string:

        return True

    else:

        return False

main_string = input("Enter the main string: ")

substring = input("Enter the substring to search: ")

if check_substring(main_string, substring):

    print(f"The substring '{substring}' is present in the main string.")

else:

    print(f"The substring '{substring}' is not present in the main string.")

#source code --> clcoding.com 

Code Explanation:

def check_substring(main_string, substring):
This line defines a function called check_substring that takes two arguments:

main_string: The string in which the program will search for the substring.
substring: The smaller string that we are trying to find inside the main_string.


    if substring in main_string:
This line checks if the substring exists within the main_string using the in operator. The in operator returns True if substring is found in main_string, and False otherwise.


        return True
If the substring is found in the main_string (i.e., the condition substring in main_string evaluates to True), this line returns True to indicate that the substring exists in the main_string.


    else:
This line begins the else block. It is executed if the substring is not found in the main_string (i.e., the condition substring in main_string evaluates to False).

        return False
If the substring is not found in the main_string, this line returns False, indicating that the substring is not present in the main_string.

main_string = input("Enter the main string: ")
This line prompts the user to input the main string (the larger string) using the input() function. The entered value is stored in the variable main_string.

substring = input("Enter the substring to search: ")
This line prompts the user to input the substring (the smaller string) to search for within the main_string. The entered value is stored in the variable substring.

if check_substring(main_string, substring):
This line calls the check_substring function with main_string and substring as arguments. The function checks if the substring exists in the main_string and returns either True or False. If the function returns True, the code inside the if block is executed.

    print(f"The substring '{substring}' is present in the main string.")
If the check_substring function returns True (indicating the substring was found), this line prints a message saying that the substring is present in the main_string. The value of substring is dynamically included in the message using an f-string.

else:
This line specifies what happens if the check_substring function returns False (i.e., if the substring is not found in the main_string). The code inside this else block will execute.

    print(f"The substring '{substring}' is not present in the main string.")
If the check_substring function returns False (indicating the substring is not found), this line prints a message saying that the substring is not present in the main_string. The value of substring is included in the message using an f-string.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (78) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (950) Python Coding Challenge (394) Python Quiz (47) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses