Change language

Python | All occurrences of a substring in a string

|

Method # 1: Using Comprehension List + startswith()
This task can be accomplished using two functions. The start with function mainly performs the task of getting the starting indices of the substring, and the list comprehension is used to iterate over the entire target string.

# Python3 demo code
# All occurrences of a substring in a string
# Using comprehension list + startwith ()

 
# initialization string

test_str = "Python.Engineering is best for Geeks"

 
# substring initialization

test_sub = "Geeks"

  # print original line

print ( "The original string is:" + test_str)

 
# print substring

print ( " The substring to find: " + test_sub)

 
# use comprehension list + startwith ()
# All occurrences of the substring in the string

res = [i for i in range ( len (test_str)) if test_str.startswith (test_sub, i)]

 
# print result

print ( "The start indices of the substrings are:" + str ( res))

Output:

 The original string is: Python.Engineering is best for Geeks The substring to find: Geeks The start indices of the substrings are: [0, 8, 26] 

Method # 2: Using re.finditer ()
The finditer function of the regex library can help us perform the task of finding occurrences of a substring in the target string, and the start function can return the resulting index of each them.

# Python3 code to demonstrate how it works
# All occurrences of a substring in the string
# Using re.finditer ()

import re

  
# initialization string

test_str = "Python.Engineering is best for Geeks"

 
# substring initialization

test_sub = " Geeks "

  
# print the original line

print  ( "The original string is:" + test_str)

 
# print substring

print ( "The substring to find:" + test_sub)

  
# using re.finditer ()
# All occurrences of a substring in a string

res = [i.start () for i in re.finditer (test_sub, test_str)]

 
# print result < / p>

print ( "The start indices of the substrings are:" + str (res))

Output:

 The original string is: Python.Engineering is best for Geeks The substring to find: Geeks The start indices of the substrings are: [0, 8, 26] 

Python | All occurrences of a substring in a string Python functions: Questions

Python | All occurrences of a substring in a string String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method