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] 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

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

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically