+ 1

A function to print the positions of a letter in a string?

for example string="sjdhsghy" h=[3, 6]

27th Dec 2016, 1:06 PM
MIGUEL GARCÍA GÓMEZ
MIGUEL GARCÍA GÓMEZ - avatar
3 Answers
+ 3
a='sjdhsghy' l=[] for i,x in enumerate(a): if x=='h': l.append(i) print(l)
27th Dec 2016, 2:04 PM
æŽć‰‘äŒŸ
æŽć‰‘äŒŸ - avatar
+ 2
It can be done in a different ways. Here, for example, recursive function, that appends existing list: string="sjdhsghy" letter="h" indeces=[] def findindeces(s, l, startindex): i=s.find(l,startindex) if(i!=-1): indeces.append(i) findindeces(s,l,i+1) findindeces(string,letter,0) print(indeces)
27th Dec 2016, 1:34 PM
Dmitry Zolnikov
Dmitry Zolnikov - avatar
- 1
hs Hudson
1st Jan 2017, 4:10 PM
Osama Muhammmed
Osama Muhammmed - avatar