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)

Explanation

  1. The string mystring[6:12] slices the string mystring starting from the 6th character (n) and continues up to, but does not include, the 12th character (a). The resulting substring is 'nexamp'.
  2. The print function then outputs this 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)

Explanation

  1. The string mystring[6:35] slices the string mystring starting from the 6th character (n) and continuing to the 35th character, but since the string length is 19, it stops at the last character (g), giving the substring 'nexamples.org'.
  2. The print function then outputs this 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)

Explanation

  1. The string mystring[-15:-5] slices the string mystring starting from the 15th character from the end (m) and ending at the 5th character from the end (r), resulting in the substring 'examples.
  2. The print function then outputs this 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)

Explanation

  1. The string mystring[:] uses slicing to extract the entire string from the beginning to the end. In this case, it results in the full string 'pythonexamples.org'.
  2. The print function then outputs the entire string.

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.


Python Libraries