Python Substring

To find substring of a string in Python, we can use slicing method.

In this tutorial, we will learn about slicing of a string, its syntax, and how to slice a given string with the help of some examples.

Syntax

The following is syntax of slicing to get substring.

string[start:stop]

where the slicing happens on string string from start to stop-1. start and stop are indices. So, please note that the index starts from 0.

start and stop are optional and can also accept any integer, both positive and negative.

If we do not provide start, the slicing of the string starts from the beginning. In other words default value of start is 0.

If we do not provide stop, the slicing of the string happens till the end of the string. In other words default value of stop is length of the string.

Python Substring
Python Substring

In this tutorial, we will go through all of the scenarios including positive, optional and negative values for start and stop parameters.

Examples

Substring with 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.

Example.py

string = 'tutorialkart'
start = 2
stop = 7
#find substring
substring = string[start:stop]
print(substring)
Try Online

Run the above program.

toria

Substring – No start 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.

Example.py

string = 'tutorialkart'
stop = 7
#find substring
substring = string[:stop]
print(substring)
Try Online

Run the above program.

tutoria

Substring – No stop 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.

Example.py

string = 'tutorialkart'
start = 5
#find substring
substring = string[start:]
print(substring)
Try Online

Run the above program.

ialkart

Substring – Start and Stop not provided

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.

Example.py

string = 'tutorialkart'
#find substring
substring = string[:]
print(substring)
Try Online

Run the above program.

tutorialkart

Substring – Negative Stop Position

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.

Example.py

string = 'tutorialkart'
start = 5
stop = -2
#find substring
substring = string[start:stop]
print(substring)
Try Online

Run the above program.

ialka

Substring – Negative Start Position

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.

Example.py

string = 'tutorialkart'
start = -10
stop = 10
#find substring
substring = string[start:stop]
print(substring)
Try Online

Run the above program.

torialka

Substring – Negative Start and Stop Positions

In this example, we provide negative values for both start and stop positions.

Example.py

string = 'tutorialkart'
start = -10
stop = -2
#find substring
substring = string[start:stop]
print(substring)
Try Online

Run the above program.

torialka

Conclusion

In this Python Tutorial, we learned how to find the substring of a string using slicing mechanism.