Substring in Python language


Python Substring

There is no dedicated function in Python to find the substring of a string. But you can use slicing to get the substring.

Using slicing, you can find the substring of a string, from a specific starting position to specific ending position.

Python Substring

Substring is part of the given string. A substring may start from a specific starting position and end at a specific ending position in the string. Sometimes, the start position of substring would be start of the original string. Or the end position of the substring would be same as that of original string.

Syntax of string slicing

The syntax to get the substring is:

mystring[start:end]

mystring[start:end] slices the string from position start until end in mystring.

start and end values are optional. If start is not provided, beginning of the original string is considered as start of substring. If end is not provided, ending of the original string is considered as end of substring.

Video Tutorial

In the following YouTube video, there is a good explanation on how to use slicing technique to find substring of a string, with examples.

https://youtu.be/BS1la64CmI8

Examples

1. Find substring with specified start and end positions in given string

In the following example, we will take a string and get the substring that starts at position 6 and spans until position 12.

Python Program

mystring = 'pythonexamples.org'

substring = mystring[6:12]
print(substring)

Output

exampl

Explanation

p  y  t  h  o  n  e  x  a  m  p  l  e  s  .  o  r  g
0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
                  ↑                 ↑
                  |←  substring  →|
                  e  x  a  m  p  l    ← resulting substring

2. Find substring with end position greater than given string length

In the following example, we will take a string and get the substring that starts at position 6 and spans until position 35. But the length of the string is 18 which is way less than the provided end of string.

In this case, length of the substring string is considered until the end position of the original string.

Python Program

mystring = 'pythonexamples.org'

substring = mystring[6:35]
print(substring)

Output

examples.org

3. Subtring with negative positions

You can also provide negative numbers for the values of start and end positions of substring.

A positive start position denotes the position from start of the main string towards end. But a negative start position denotes the position from end of the main string towards start.

In the following example, we shall find the substring of a string.

Python Program

mystring = 'pythonexamples.org'

substring = mystring[-15:-5] #mystring[18-15:18-5] where length of mystring is 18
print(substring)

Output

honexample

4. Substring when no start or end positions provided

If we do not provide any start or end position, the slicing of the string returns the original string.

In the following example, we shall try to slice a string with no start of end position. The resulting substring should be same as that of the original string.

Python Program

mystring = 'pythonexamples.org'

substring = mystring[:]
print(substring)

Output

pythonexamples.org

Summary

In this tutorial of Python Examples, we learned to find the substring of a string in Python using slicing with the help of well detailed examples.