A string in Python is a sequence of different characters, and a substring is a sequence of characters inside the string. There are methods by which we can create substring, check substring existence, substring occurrence, substring index, etc.
This article will be doing all the operations mentioned above in different ways with various methods. So, follow along.
Table of Contents
How to Retrieve substring from an original String in Different Ways?
Let’s create a substring using the slicing technique. In slicing, we need to provide the index of a substring in parenthesis.
Let’s have a look at some examples.
Example #01: How to Retrieve String by Specifying Starting and Ending Point?
Slicing the string by specifying the starting index and the ending index of the string will return the substring, which lies in the index range.
While providing the index range for slicing, the first index number is inclusive, while the second is exclusive. The substring will start to fetch from index 5 till the letter at index 8 and not 9.
Code:
string = "Code Leaks"
print("String = " , string)
substring = string[5:9]
print("string[5:9] = " , substring)
Output:

Example #02: How to Retrieve String from Starting Point only?
Code:
string = "Code Leaks"
print("String = " , string)
substring = string[5:]
print("string[5:] = " , substring)
Output:

Example #03: How to Retrieve String from Ending Point only?
Code:
string = "Code Leaks"
print("String = " , string)
substring = string[:5]
print("string[:5] = " , substring)
Output:

Example #04: How to Retrieve String without Specifying Index Number?
Code:
string = "Code Leaks"
print("String = " , string)
substring = string[:]
print("string[:] = " , substring)
Output:

How to Check the Existence of a Substring in a String in Different Ways?
Method #01: Using ‘in’ Operator
Code:
string = "Code Leaks."
substring = "Leak"
if substring in string:
print("Substring Exists")
else:
print("Substring Not Exists")
Output:

Method #02: Using String.index() Method
Code:
string = "Code Leaks"
substring = "Leak"
try:
string.index(substring)
except ValueError:
print("Substring not Exists")
else:
print("Substring Exists")
Output:

Method#03: Using String.find() Method [ substring existence at particular index ]
Code:
fullstring = "Code Leaks"
substring = "Leak"
if fullstring.find(substring) == 1:
print("Substring Exist")
else:
print("Substring not Exist")
Output:

Method#04 Using Regular Expression Search Module
Code:
from re import search
string = "Code Leaks"
substring = "Leak"
if search(substring, string):
print("Substring Exists")
else:
print("Substring not Exists")
Output:

How to Check the Occurrence of a Substring in a String?
Let’s check the number of occurrences of a substring within a String. We can do this using count() function.
Example #01
Code:
string = "Code Leaks"
substring = string.count("e")
print("e occurs " , substring , "times in string")
Output:

How to Find the Index of a Substring in a String?
Example #01
Code:
def find_all_indexes(string, substring):
index_list = []
length = len(string)
index = 0
while index < length:
i = string.find(substring, index)
if i == -1:
return index_list
index_list.append(i)
index = i + 1
return index_list
string = "Code Leaks"
print("e occurs at the indexes " , find_all_indexes(string, 'e'))
Output:

Conclusion
In this detailed article, we went through different ways to handle a substring of a string in Python. We tried performing multiple operations over it, such as creating a substring, checking its existence, its occurrence, finally finding out the substring index.
If you are a beginner struggling with dealing substring in Python, then this guide is just the right place for you to follow and learn.