Python Program to Check if a Substring is Present in a Given String

Strings in Python:

“String is a character collection or array”

Well in Python too, for the string data type, we say the same definition. The string is a sequenced character array and is written within single, double, or three quotes. Also, Python does not have the data type character, thus it is used as a string of length 1 if we write ‘V’.

Substring in Python:

A substring in Python is a sequence of characters within another string. It is also known as string slicing in Python.

Given a string and substring, the task is to check whether the given substring exists in the given string or not in Python

Examples:

Example1:

Input:

given string = hello this is BTechgeeks 
given substring = BTechgeeks

Output:

The substring [ BTechgeeks ] is found in [ hello this is BTechgeeks ]

Example2:

Input:

given string = good morning virat
given substring = morning

Output:

The substring [ morning ] is found in [ good morning virat ]

Program to Check if a Substring is Present in a Given String in Python

There are several ways to check if the given substring exists in a given string or not in Python some of them are:

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Method #1:Using find() function (Static Input)

Approach:

  • Give string and substring as static input and store them in two separate variables.
  • Using the in-built find() method, determine whether the substring exists in the given string.
  • If the find() function returns -1 then the given substring isn’t found in the given string.
  • Else the given substring is found in the given string
  • The Exit of the program.

Below is the implementation:

# Give string and substring as static input and store them in two separate variables
given_strng = 'hello this is BTechgeeks'
# given substring
given_substring = 'BTechgeeks'
# Using the in-built find() method, determine
# whether the substring exists in the given string.
# If the find() function returns -1 then the given substring isn't found in the given string.
if(given_strng.find(given_substring) == -1):
    print('The substring [', given_substring,
          '] is not found in [', given_strng, ']')
else:
    print('The substring [', given_substring,
          '] is found in [', given_strng, ']')

Output:

The substring [ BTechgeeks ] is found in [ hello this is BTechgeeks ]

Method #2:Using find() function (User Input)

Approach:

  • Give string and substring as user input using input() function and store them in two separate variables.
  • Using the in-built find() method, determine whether the substring exists in the given string.
  • If the find() function returns -1 then the given substring isn’t found in the given string.
  • Else the given substring is found in the given string
  • The Exit of the program.

Below is the implementation:

# Give string and substring as user input using input() function
# and store them in two separate variables.
given_strng = input('Enter some random string = ')
# given substring
given_substring = input('Enter some random substring = ')
# Using the in-built find() method, determine
# whether the substring exists in the given string.
# If the find() function returns -1 then the given substring isn't found in the given string.
if(given_strng.find(given_substring) == -1):
    print('The substring [', given_substring,
          '] is not found in [', given_strng, ']')
else:
    print('The substring [', given_substring,
          '] is found in [', given_strng, ']')

Output:

Enter some random string = good morning virat
Enter some random substring = morning
The substring [ morning ] is found in [ good morning virat ]

Explanation:

  • A string and a substring from the user must be entered and stored in separate variables.
  • The in-built method find() is then used to see if the substring is present in the string.
  • The decision is made using an if statement, and the final result is printed.

Method #3:Using in operator (Static Input)

Approach:

  • Give string and substring as static input and store them in two separate variables.
  • Using the in operator, determine whether the substring exists in the given string.
  • If it returns true, then the given substring is found in the given string.
  • Else the given substring isn’t found in the given string
  • The Exit of the program.

Below is the implementation:

# Give string and substring as static input and store them in two separate variables.
given_strng = 'hello this is BTechgeeks'
# given substring
given_substring = 'BTechgeeks'
# Using the in operator, determine whether the substring exists in the given string
# If it returns true, then the given substring is found in the given string.
if given_substring in given_strng:
    print('The substring [', given_substring,
          '] is  found in [', given_strng, ']')
# Else the given substring isn't found in the given string
else:
    print('The substring [', given_substring,
          '] is not found in [', given_strng, ']')

Output:

The substring [ BTechgeeks ] is  found in [ hello this is BTechgeeks ]

Method #4:Using in operator (User Input)

Approach:

  • Give string and substring as user input using input() function and store them in two separate variables.
  • Using the in operator, determine whether the substring exists in the given string.
  • If it returns true, then the given substring is found in the given string.
  • Else the given substring isn’t found in the given string
  • The Exit of the program.

Below is the implementation:

# Give string and substring as user input using input() function
# and store them in two separate variables.
given_strng = input('Enter some random string = ')
# given substring
given_substring = input('Enter some random substring = ')
# Using the in operator, determine whether the substring exists in the given string
# If it returns true, then the given substring is found in the given string.
if given_substring in given_strng:
    print('The substring [', given_substring,
          '] is  found in [', given_strng, ']')
# Else the given substring isn't found in the given string
else:
    print('The substring [', given_substring,
          '] is not found in [', given_strng, ']')

Output:

Enter some random string = this is that
Enter some random substring = are
The substring [ are ] is not found in [ this is that ]

Related Programs: