Issue
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
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.
Answered By - The Myth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.