In this Python tutorial, we will learn how to find the substring of a string using string slicing technique, the syntax used for slicing, and how to slice a given string from a specific starting position to specific ending position, with the help of some examples.
Python – Substring
To find substring of a string in Python, you can use string slicing technique.
Syntax
The syntax for slicing a string to get the substring defined by a specific start index and end index is given in the following.
mystring[start:stop]
where the slicing happens on mystring
string from start
index to stop-1
index. start
and stop
are indices of characters in the string. Please note that the index of characters in the string start from 0
.
start
and stop
are optional and can accept any integer, both positive and negative.
If we do not provide start
, the slicing of the string defaults to the beginning of the given string. In other words default value of start
is 0
.
If we do not provide stop
, the slicing of the string defaults to till the end of the string. In other words default value of stop
is length of the string.
Examples
In the following examples, we will go through the scenarios where the indices: start
and stop
; can be positive, optional, or negative values.
1. Substring with specific start and stop
In this example, we shall initialize a string, and find the substring that starts from a specific start position and ends at specific end position.
Python Program
string = 'tutorialkart'
start = 2
stop = 7
#find substring
substring = string[start:stop]
print(substring)
Run the above program.
toria
2. Substring with only stop index specified
In this example, we shall initialize a string, and find the substring by specifying only stop position. We are leaving the start parameter empty. So, the slicing of string should happen from beginning of the string. Let us see.
Python Program
string = 'tutorialkart'
stop = 7
#find substring
substring = string[:stop]
print(substring)
Run the above program.
tutoria
3. Substring with only start index specified
In this example, we shall initialize a string, and find the substring by specifying only start position. We do not provide any value for start parameter. So, the slicing of string should happen from specified start value till the ending of the string.
Python Program
string = 'tutorialkart'
start = 5
#find substring
substring = string[start:]
print(substring)
Run the above program.
ialkart
4. Substring with default start and stop indices
Let us not provide any start or stop positions. We know that if start is not provided, slicing happens from starting of the string. Also, if no stop position is provided, the slicing happens till the end of the string. There if we do not provide any start or stop positions, the resulting substring will be just a copy of the original string.
Python Program
string = 'tutorialkart'
#find substring
substring = string[:]
print(substring)
Run the above program.
tutorialkart
5. Substring with negative stop index
We can provide a negative number for stop parameter. Negative position mean that you have to consider the end of the string to get to calculate the position.
In the following example, we have taken stop as -2
. So, resulting stop would be len(string)-2
.
Python Program
string = 'tutorialkart'
start = 5
stop = -2
#find substring
substring = string[start:stop]
print(substring)
Run the above program.
ialka
6. Substring with negative start index
As discussed in the previous example, the same explanation holds for the negative start value to find the substring.
start = -10
means start = len(string)-10
.
Python Program
string = 'tutorialkart'
start = -10
stop = 10
#find substring
substring = string[start:stop]
print(substring)
Run the above program.
torialka
7. Substring with both negative start and negative stop indices
In this example, we provide negative values for both start and stop positions.
Python Program
string = 'tutorialkart'
start = -10
stop = -2
#find substring
substring = string[start:stop]
print(substring)
Run the above program.
torialka
Conclusion
In this Python Tutorial, we learned how to find the substring of a string using slicing mechanism.