Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding indexes of substrings

I am trying to code a program to find the index of multiple substrings of a string but I am stuck! See the examples bellow:

Find : 'yes'
Input = 'adnyesdapodyesndudndnyesae'
Output = [3,11,21]
Find : 'b'
Input = 'bbbbbbb'
Output = [0,1,2,3,4,5,6]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

str1 = "adnyesdapodyesndudndnyesae" #The String
substr = "yes" #The Substring
res = [i for i in range(len(str1)) if str1.startswith(substr, i)]
print(str(res))

The third line basically runs a list comprehension method wherein using a for loop you check for occurences of a substring in the string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading