Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python String Substring

Substring is a portion of the string that can be extracted and represented as a new string. In this tutorial, we will learn how to work with Substrings in Python.

1. Creating Python String Substring

Substring can be created using either of the following methods:

  • Slicing
  • split() method
input = 'Engineering Field'

#substring using slice
result = input[5:]
print(result)

#substring using split
result1 = input.split()
print(result1)

Output:

Creation-Substring
Creation-Substring
Substring Creation Example
Substring Creation Example

2. Check for the presence of a substring in a String

The presence of sub-string in a string can be checked by either of the following methods:

  • By using in operator
  • By using find() method
input = 'Medical Colleges in India.'
#using in operator
if 'India' in input:
    print('Substring is present in the input.')
#using find() method
if input.find('Medical Colleges') != -1:
    print('Substring is present in the input.')

Output:

Substring is present in the input.
Substring is present in the input.

Substring Presence Example
Substring Presence Example

3. Get the substring from a String in Python

The following are some of the methods to get the substring from a given string:

  • By using list slicing
  • By using itertools.combinations() method

Example 1: By using List Slicing

str = 'Engineering Discipline'
  
sub1 = str[:3] 
sub2 = str[6:] 
  

print ("Resultant substring from the start: ", sub1) 
print ("Resultant substring from the end: ", sub2) 

Output:

Output Fetch Substring
Output-Fetch Substring

Example 2: By using itertools.combinations() method

from itertools import combinations 
  

str1 = "Safa"
  

print("The original string is : " + str(str1)) 
  
 
result = [str1[a:b] for a, b in combinations( 
            range(len(str1) + 1), r = 2)] 
  

print("All substrings of string are : " + str(result)) 

Output:

Output Fetch Substring 1
Output-Fetch Substring 1

4. Count of occurrence of a substring in a String

The count() function is used to represent the count of the characters/sub-string in the given string.

string = 'Engineering Discipline'

print('Count of Sub-string =', string.count('i'))

string1 = 'Aroma Arena'
print('Count of Sub-string =', string1.count('Ar'))

Output:

Output Count Substring
Output Count Substring
Count Substring
Count Substring

5. Get the indexes of all substring

def find_index(str1, substring):
    l2 = []
    length = len(str1)
    index = 0
    while index < length:
        x = str1.find(substring, index)
        if x == -1:
            return l2
        l2.append(x)
        index = x + 1
    return l2


string = 'Aroma Arena at the desert Arone.'
print(find_index(string, 'Ar'))

The above snippet of code gives us the index/position of all of the occurrence of a particular substring in a string.

Output:

[0, 6, 26]


References