Python Program to Check 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, string and substring must be received by user at run-time of the program. 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 the sample run of this program, with user input Python is easy to learn as string and easy as substring:

python check substring is in string

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

But the problem with above program is, sometime user needs to check whether substring is in the given string or not, with or without caring the uppercase/lowercase characters. Therefore, we need to create a program, that gives options to user 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 string, Fun as substring, and 2 as choice to check the availability of given phrase in given string, without caring of uppercase/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 first two inputs as same to above sample run, and the choice as 1:

python check if substring in given string

Python to Check if a Substring is not in a String

This is the reverse of 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 jobails.com as string and org as phrase to confirm whether it is not available in the given string, or it is in the given string:

python check if a substring is not in given string

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

Python Online Test


« Previous Program Next Program »