Python program to check if a substring is present in a given string

This article is created to cover a program in Python that checks whether a specified substring (a character or a phrase) is present in a given string or not. This article contains two programs:

  1. Check if a substring is available in a given string.
  2. Check if a substring is not available in a given string.

Python to check if a substring is in a string

The question is: write a Python program to check if a substring is available in a string. The value of both strings and substrings must be received by the user at runtime of the program. The answer to this question is the program given below:

print("Enter the String: ", end="")
MyStr = input()

print("\nEnter the Substring: ", end="")
MySubStr = input()

if MySubStr in MyStr:
    print("\nYes,\nThe substring is available in the string.")
else:
    print("\nNo,\nThe substring is not available in the string.")

The snapshot given below shows a sample run of this program with user input, "Python is easy to learn" as a string and "easy" as a substring:

python check substring is in string

Note: The in operator is used to check the availability of a specified value in a specified sequence.

But the problem with the above program is that sometimes the user needs to check whether a substring is in the given string or not, with or without caring about the uppercase or lowercase characters. Therefore, we need to create a program that gives users options on how to proceed using both. For example:

print("Enter the String: ", end="")
MyStr = input()
print("Enter the Substring: ", end="")
MySubStr = input()

print("\n1. Uppercase/Lowercase matters")
print("2. Uppercase/Lowercase doesn't matters")
print("Enter Your Choice (1 or 2): ", end="")
choice = int(input())

if choice == 1:
    if MySubStr in MyStr:
        print("\nYes,\nThe substring is available in the string.")
    else:
        print("\nNo,\nThe substring is not available in the string.")

elif choice == 2:
    if MySubStr.lower() in MyStr.lower():
        print("\nYes,\nThe substring is available in the string.")
    else:
        print("\nNo,\nThe substring is not available in the string.")

else:
    print("\nInvalid Choice!")

The sample run with user input "Python is fun" as a string, "Fun" as a substring, and "2" as a choice to check the availability of a given phrase in a given string without caring about uppercase or lowercase letters is shown in the snapshot given below:

python check substring is present in given string

And the following snapshot shows another sample run, with the first two inputs being the same as the above sample run and the choice being "1":

python check if substring in given string

Python to check if a substring is not in a string

This is the reverse of the above program. That is, this program checks whether a specified substring is not in a string or in the string. To do this task, use not before "in", that is, "not in", instead of "in". For example:

print("Enter the String: ", end="")
x = input()
print("Enter the Substring: ", end="")
y = input()

if y not in x:
    print("\n'", y, "' is not available in '", x, "'", sep="")
else:
    print("\n'", y, "' is available in '", x, "'", sep="")

Here is its sample run with user input "codescracker.com" as a string and "org" as a phrase to confirm whether it is not available in the given string or whether it is in the given string:

python check if a substring is not in given string

The second program from the previous section can also be created in a similar way in this section too. The only difference is that we need to replace "in" with "not in". That's it. Come on, do it yourself.

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!