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