Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Find All Occurrences of start indices of the substrings in a String in Python

# defining string 
str1 = "This dress looks good; you have good taste in clothes."
  
# defining substring
substr = "good"
  
# printing original string 
print("The original string is : " + str1)
  
# printing substring 
print("The substring to find : " + substr)
  
# using list comprehension + startswith()
# All occurrences of substring in string 
res = [i for i in range(len(str1)) if str1.startswith(substr, i)]
  
# printing result 
print("The start indices of the substrings are : " + str(res))

# Output -
# The original string is : This dress looks good; you have good taste in clothes.
# The substring to find : good
# The start indices of the substrings are : [17, 34]
Comment

PREVIOUS NEXT
Code Example
Python :: python class to tuple 
Python :: prime number program in python using function 
Python :: programme phyton pour realiser un programme qui transforme une image en niveau de gris 
Python :: select series of columns 
Python :: Dynamically limiting queryset of related field 
Python :: Load Data From JSON PYQT5 
Python :: Python - Comment vérifier une corde contient un nombre 
Python :: df filter by multiple rules python 
Python :: off-by-one error in python 
Python :: Constructing a Class with __init__ function 
Python :: how to send jobs to queue dynamically 
Python :: youtube view bot python code pastebin 
Python :: is complex datatype immutable in python 
Python :: compute slice distance from image position 
Python :: vscode how to extend output size in jupyter notebook 
Python :: email slicer in python code user input 
Python :: python .exe long start 
Python :: workbook select sheet python 
Python :: networkx select edge 
Python :: what if init migrations run two times or by pass this migrate 
Python :: computecost pyspark 
Python :: colorama input python 
Python :: bogo sort 
Python :: pivot_table value aggfunct 
Python :: python3 main.py 
Python :: how to get max id in mongodb python 
Python :: count each value in lsitp ython 
Python :: datetime german format python 
Python :: how do i select a range of columns by index 
Python :: python 3.10 windows 7 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =