+ 1
A function to print the positions of a letter in a string?
for example string="sjdhsghy" h=[3, 6]
3 Antworten
+ 3
a='sjdhsghy'
l=[]
for i,x in enumerate(a):
if x=='h':
l.append(i)
print(l)
+ 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)
- 1
hs Hudson