Exercise 72

Write a python function which examine if a particular substring occurs within a given string.


the function is_substring_present takes two arguments: string, which represents the given string, and sub, which represents the substring that we want to check if it occurs within string.
# Python function to check if a particular substring occurs within a given string
def is_substring_present(string, sub):
    if sub in string:
        return True
        return False

# Example of use of this algorithm

string = "Python programming language"
sub = "prog"

if is_substring_present(string, sub):
    print(f"{sub} is present in {string}")
    print(f"{sub} is not present in {string}")
# output: 'prog' is present in 'Python programming language'


Younes Derfoufi

One thought on “Solution Exercise 72 : python algorithm to examin if a substring is present within a given string”

Leave a Reply